Add Book to My BookshelfPurchase This Book Online

Chapter 3 - Synchronizing Pthreads

Pthreads Programming
Bradford Nichols, Dick Buttlar and Jacqueline Proulx Farrell
 Copyright © 1996 O'Reilly & Associates, Inc.

Selecting the Right Synchronization Tool
You can choose from among many Pthreads functions to obtain some type of synchronization:
pthread_join function
pthread_join allows one thread to suspend execution until another has terminated. We discussed the pthread_join function in Chapter 1, Why Threads?
Mutex variable functions
A mutex variable acts as a mutually exclusive lock, allowing threads to control access to data. The threads agree that only one thread at a time can hold the lock and access the data it protects. We'll discuss mutex variables in this chapter.
Condition variable functions
A condition variable provides a way of naming an event in which threads have a general interest. An event can be something as simple as a counter's reaching a particular value or a flag being set or cleared; it may be something more complex, involving a specific coincidence of multiple events. Threads are interested in these events, because such events signify that some condition has been met that allows them to proceed with some particular phase of their execution. The Pthreads library provides ways for threads both to express their interest in a condition and to signal that an awaited condition has been met. We'll discuss condition variables in this chapter.
pthread_once function
pthread_once is a specialized synchronization tool that ensures that initialization routines get executed once and only once when called by multiple threads. We'll discuss the pthread_once function in Chapter 4, Managing Pthreads.
These synchronization tools provide all that you need to write almost any program you can imagine. We can safely say that you can create whatever complex synchronization tools you may need from these basic building blocks. Some of the common synchronization mechanisms are:
Reader/writer exclusion
Reader/writer locks allow multiple threads to read data concurrently but ensure that any thread writing to the data has exclusive access.
Threadsafe data structures
You may find it useful to build synchronization primitives into a complex data structure so that each time you access it you don't need to make a separate call to synchronize concurrent access. For instance a queue library may include enqueue and dequeue functions that transparently include synchronization calls.
Semaphores
If your platform supports POSIX real-time extensions (POSIX.1b), you can take advantage of yet another common synchronization primitive for concurrent environments—semaphores. A counting semaphore is like a mutex but is associated with a counter. If your platform supports both the POSIX real-time extensions and Pthreads, you can use semaphores on a per-thread basis in the same way you would use a mutex*. We'll briefly discuss semaphores in Chapter 5, Pthreads and UNIX.
 *A full discussion of semaphores is beyond the scope of this book. For a detailed discussion of all of the POSIX real-time extensions, see the book POSIX.4:Programming for the Real World by Bill O. Gallmeister from O'Reilly & Associates.
Later in this chapter we'll provide examples of a thread safe linked list and a reader/writer lock implementation. These will give you a idea of what it is like to implement higher-level synchronization facilities on top of the standard Pthreads ones.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback