Critical Section(关键区)

关键去指访问了共享资源的代码块,共享资源通常是一个变量或者一个数据结构。

Race Condition/Data Race(竞争条件)

它旨在描述一个系统或者进程的输出依赖于不受控制的事件出现顺序或者出现时机。

Mutual Exclusion(互斥)

是一种为了防止竞争条件发生的并行控制。

Condition Variable(条件变量)

A condition variable is basically a container of threads that are waiting for a certain condition.

When the condition expression is false, the thread blocks on the condition variable. The condition variable is then signaled by another thread when it changes the condition value. This causes one or all of the threads waiting on the condition to unblock and to try to acquire the mutex lock again.

Thread 1:

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; 
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
Pthread_mutex_lock(&lock); 
while (ready == 0) 
    Pthread_cond_wait(&cond, &lock); 
Pthread_mutex_unlock(&lock);

Thread 2:

Pthread_mutex_lock(&lock); 
ready = 1; 
Pthread_cond_signal(&cond); 
Pthread_mutex_unlock(&lock);