Sun Microsystems Logo
Products and Services
 
Support and Training
 
 

Previous Previous     Contents     Index     Next Next
Chapter 4

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:

  • Mutex Locks

  • Condition Variables

  • Read-Write Locks

  • Semaphores

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

Operation

Destination Discussion

 

Initialize a mutex attribute object

pthread_mutexattr_init(3THR)

 

Destroy a mutex attribute object

pthread_mutexattr_destroy(3THR)

 

Set the scope of a mutex

pthread_mutexattr_setpshared(3THR)

 

Get the scope of a mutex

pthread_mutexattr_getpshared(3THR)

 

Set the mutex type attribute

pthread_mutexattr_settype(3THR)

 

Get the mutex type attribute

pthread_mutexattr_gettype(3THR)

 

Set mutex attribute's protocol

pthread_mutexattr_setprotocol(3THR)

 

Get mutex attribute's protocol

pthread_mutexattr_getprotocol(3THR)

 

Set mutex attribute's priority ceiling

pthread_mutexattr_setprioceiling(3THR)

 

Get mutex attribute's priority ceiling

pthread_mutexattr_getprioceiling(3THR)

 

Set mutex's priority ceiling

pthread_mutex_setprioceiling(3THR)

 

Get mutex's priority ceiling

pthread_mutex_getprioceiling(3THR)

 

Set mutex's robust attribute

pthread_mutexattr_setrobust_np(3THR)

 

Get mutex's robust attribute

pthread_mutexattr_getrobust_np(3THR)

 

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

Solaris

POSIX

Definition

USYNC_PROCESS

PTHREAD_PROCESS_SHARED

Use to synchronize threads in this and other processes

USYNC_PROCESS_ROBUST

No POSIX equivalent

Use to robustly synchronize threads between processes

USYNC_THREAD

PTHREAD_PROCESS_PRIVATE

Use to synchronize threads in this process only

Previous Previous     Contents     Index     Next Next