Go to main content

Multithreaded Programming Guide

Exit Print View

Updated: November 2020
 
 
Chapter 4

Programming with Synchronization Objects

Synchronization objects are variables in memory that you access just like data. Threads in different processes can communicate with each other through synchronization objects that are placed in threads-controlled shared memory. The threads can communicate with each other even though the threads in different processes are generally invisible to each other.

Synchronization objects can also be placed in files. The synchronization objects can have lifetimes beyond the life of the creating process.

    The available types of synchronization objects are:

  • Mutex locks

  • Condition variables

  • Read-Write locks

  • Semaphores

    Situations that can benefit from the use of synchronization include the following:

  • Synchronization is the only way to ensure consistency of shared data.

  • Threads in two or more processes can use a single synchronization object jointly. Because reinitializing a synchronization object sets the object to the unlocked state, the synchronization object should be initialized by only one of the cooperating processes.

  • Synchronization can ensure the safety of mutable data.

  • A process can map a file and direct a thread in this process get a record's lock. Once the lock is acquired, any thread in any process mapping the file attempting to acquire the lock is blocked until the lock is released.

  • Accessing a single primitive variable, such as an integer, can use more than one memory cycle for a single memory load. More than one memory cycle is used where the integer is not aligned to the bus data width or is larger than the data width. While integer misalignment cannot happen on the SPARC architecture, portable programs cannot rely on the proper alignment.


Note -  On 32-bit architectures, a long long is not atomic. (An atomic operation cannot be divided into smaller operations.) A long long is read and written as two 32-bit quantities. The types int, char, float, and pointers are atomic on SPARC architecture machines and Intel Architecture machines.