CITS2002 Systems Programming  
next CITS2002 CITS2002 schedule  

Introduction to synchronization using mutexes

A core problem with threads sharing the same memory address space, is making sure that they don't "step on each other's toes":

  • if multiple threads are simply reading the same variable, there is no problem.

  • however, if multiple threads update the same variable, the order of those updates - the order in which the threads execute - is important as it affects the values of the variable, over time, and the final value of that variable.

Thread libraries support an abstraction named a mutex, an abbreviation of mutual exclusion. Mutex variables are one of the primary means providing synchronization of threads - the order in which they execute - and for protecting shared data when multiple writes can occur.

A mutex variable acts like a lock protecting access to a shared data resource.

The basic concept of a mutex as used in pthreads is that only one thread can lock (or own) a mutex variable at any given time.

Thus, even if several threads try to lock a mutex only one thread will be successful. No other thread can own that mutex until the owning thread unlocks that mutex. Threads must take turns accessing protected data.

 


CITS2002 Systems Programming, Lecture 21, p1, 10th October 2023.