Thread Synchronization

  • Most important difference between multi-threaded and single-threaded programs:

    • 2 operations in a single-threaded program are always executed sequentially

    • 2 operations in a multi-threaded program can be executed in parallel

  • Fact:

    • Some operations are conflicting

    • conflicting operations cannot be executed simultaneously

  • Example of a conflicting operation:

    • Write/Write conflict:

      • Thread 1 increments the shared variable N
      • Thread 2 increments the shared variable N

DEMO: demo/pthread/sharing-vars2.c

Synchronization problems and synchronization mechanisms

  • There are many different kinds of synchronization problems in programming:

    • Critical section problem
    • Producer-consumer problem
    • Dining philosophers problem
    • ...

  • Synchronization problems are solved using:

    • a one or a combination of several synchronization mechanisms

  • The various synchronization mechanisms developed include:

    • Mutex lock (or Mutually exclusive lock)
    • Read/Write lock
    • Binary semaphore
    • Counting semaphore
    • Barrier
    • Conditional variable


  • In CS355, we will only study one of these techniques: mutex lock

    (Take a course in parallel programming for the full treatment...)