Brief

  • Locks: Atomicity
  • Condition Variables: Ordering
    • Fork/Join
    • Producer/Consumer

Semaphore

  • Lock + CV in one

Mutual Exclusion

  • Lock version
    lock();
    crtical section
    unlock();
    
  • Semaphore Version
    sem_wait(&lock)
    critical section
    sem_post(&lock);
    
    • Initialization: to use semaphore as lock
      • Initialize it to 1

Fork/Join

  •  child() {
         ...
         sem_post(&s);
     }
    
     main() {
         sem_init(&s, 0);
         pthread_create(child);
         sem_wait(&s);
     }
    

Reader/Writer

  • Starvation