![]() |
![]() |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Chapter 4Programming with Synchronization ObjectsThis chapter describes the synchronization types available with threads and discusses when and how to use synchronization. Synchronization objects are variables in memory that you access just like data. Threads in different processes can communicate with each other through synchronization objects placed in threads-controlled shared memory, even though the threads in different processes are generally invisible to each other. Synchronization objects can also be placed in files and can have lifetimes beyond that of the creating process. The available types of synchronization objects are:
Here are situations that can benefit from the use of synchronization:
Note - On 32-bit architectures a long long is not atomic [An atomic operation cannot be divided into smaller operations.] and is read and written as two 32-bit quantities. The types int, char, float, and pointers are atomic on SPARC Platform Edition machines and Intel Architecture machines. Mutual Exclusion Lock AttributesUse mutual exclusion locks (mutexes) to serialize thread execution. Mutual exclusion locks synchronize threads, usually by ensuring that only one thread at a time executes a critical section of code. Mutex locks can also preserve single-threaded code. To change the default mutex attributes, you can declare and initialize an attribute object. Often, the mutex attributes are set in one place at the beginning of the application so they can be located quickly and modified easily. Table 4-1 lists the functions discussed in this section that manipulate mutex attributes. Table 4-1 Mutex Attributes Routines
The differences between Solaris threads and POSIX threads, when defining the scope of a mutex, are shown in Table 4-2. Table 4-2 Mutex Scope Comparison
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||