Locking Mechanisms for Threads

Since Threads share the global heap, initialized data and uninitialized data segment with one another, it is possible that there would be race conditions between two threads. To prevent race conditions and to set up synchronization between the different threads, POSIX supports the following locking or thread synchronization mechanisms.

POSIX Semaphores:

  • named semaphores
    • The semaphore has a name. by calling sem_open using the name of the semaphore, different threads can use the same semaphore
  • unnamed semaphores
    • The semaphore does not have a name. The semaphore resides in an agreed upon location in memory. when shared between threads, the semaphore needs to reside in a memory mapped region (mmap ()) shared memory.

The semaphores have a count value and if the semaphore value is zero, the calling process will block until the semaphore value increments above zero. Semaphores can also be used between different processes and not just limited to threads though the article built upon this topic based on thread operation in Linux.

In the following articles, we will look at named semaphores and unnamed semaphores

Named Semaphore in Linux user space

Comments

  1. Pingback: Simple Thread Example | Hitch Hiker's Guide to Learning

Leave a Reply

Your email address will not be published. Required fields are marked *