Programming with Synchronization Objects
This 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:
When synchronization is the only way to ensure consistency of shared data.
When threads in two or more processes can use a single synchronization object jointly. Note that the synchronization object should be initialized by only one of the cooperating processes, because reinitializing a synchronization object sets it to the unlocked state.
When synchronization can ensure the safety of mutable data.
When a process can map a file and have a thread in this process get a record's lock. Once the lock is acquired, any other thread in any process mapping the file that tries to acquire the lock is blocked until the lock is released.
Even when accessing a single primitive variable, such as an integer. On machines where the integer is not aligned to the bus data width or is larger than the data width, a single memory load can use more than one memory cycle. While this cannot happen on the SPARC® Platform Edition architecture, portable programs cannot rely on this.
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 Attributes
Use 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
Initialize a mutex attribute object
Destroy a mutex attribute object
Set the scope of a mutex
Get the scope of a mutex
Set the mutex type attribute
Get the mutex type attribute
Set mutex attribute's protocol
Get mutex attribute's protocol
Set mutex attribute's priority ceiling
Get mutex attribute's priority ceiling
Set mutex's priority ceiling
Get mutex's priority ceiling
Set mutex's robust attribute
Get mutex's robust attribute
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
Use to synchronize threads in this and other processes
No POSIX equivalent
Use to robustly synchronize threads between processes
Use to synchronize threads in this process only