Hardware Reference
In-Depth Information
Thread call
Meaning
pthread create
Create a new thread in the called's address space
pthread exit
Terminate the calling thread
pthread join
Wait for a thread to terminate
pthread mutex init
Create a new mutex
pthread mutex destroy
Destroy a mutex
pthread mutex lock
Lock a mutex
pthread mutex unlock
Unlock a mutex
pthread cond init
Create a condition variable
pthread cond destroy
Destroy a condition variable
pthread cond wait
Wait on a condition variable
pthread cond signal
Release one thread waiting on a condition variable
Figure 6-44. The principal POSIX thread calls.
Mutexes can be created and destroyed by the calls pthread mutex init and
pthread mutex destroy , respectively. A mutex can be in one of two states: locked
or unlocked. When a thread needs to set a lock on an unlocked mutex (using
pthread mutex lock ), the lock is set and the thread continues. However, when a
thread tries to lock a mutex that is already locked, it blocks. When the locking
thread is finished with the shared resource, it is expected to unlock the correspond-
ing mutex by calling pthread mutex unlock .
Mutexes are intended for short-term locking, such as protecting a shared vari-
able. They are not intended for long-term synchronization, such as waiting for a
tape drive to become free. For long-term synchronization, condition variables are
provided. These are created and destroyed by calls to pthread cond init and
pthread cond destroy , respectively.
A condition variable is used by having one thread wait on it, and another signal
it. For example, having discovered that the tape drive it needs is busy, a thread
would do pthread cond wait on a condition variable that all the threads have
agreed to associate with the tape drive. When the thread using the tape drive is
finally done with it (possibly hours later), it uses pthread cond signal to release
exactly one thread waiting on that condition variable (if any). If no thread is wait-
ing, the signal is lost. Condition variables do not count like semaphores. A few
other operations are also defined on threads, mutexes, and condition variables.
Windows 7 Process Management
Windows 7 supports multiple processes, which can communicate and synchro-
nize. Each process contains at least one thread. Together, processes and threads
(which can be scheduled by the process itself) provide a general set of tools for
 
Search WWH ::




Custom Search