前兩天寫了個簡單的線程池,結果在處理線程同步互斥上花了不少時間,於是決定把以前學的知識重新過一遍。這次複習的是四個非常經典的同步互斥問題。
問題描述:只有緩衝區沒滿時,生產者才能放入消息,否則必須等待;只有緩衝區不空時,消費者才能取出消息,否則必須等待。由於緩衝區是臨界資源,它只允許一個生產者放入或一個消費者取出。
分析:生產者與消費者對緩衝區的訪問是互斥關係,同時兩者又是協作(同步)關係——只有生產之後才能消費。需要解決的是互斥與同步的 PV 操作位置。
信號量設置:互斥鎖 mutex 控制互斥訪問;full 記錄「滿」緩衝區數,初值 0;empty 記錄「空」緩衝區數,初值 n。
#define N 10
sem_t empty; //空閒緩衝區信號量,初始化為 N
sem_t full; //滿緩衝區信號量,初始化為 0
pthread_mutex_t mutex; //對緩衝區互斥訪問
int product_num = 0; //緩衝區產品數目
void *producter_f(void *arg)
{
while (1) {
usleep(1000000);
printf("produce an item\n");
sem_wait(&empty); //獲取空緩衝區單元
pthread_mutex_lock(&mutex); //進入臨界區
product_num++;
printf("producter: the num of product is %d\n", product_num);
pthread_mutex_unlock(&mutex);//離開臨界區
sem_post(&full); //滿緩衝區加 1
}
}
void *consumer_f(void *arg)
{
while (1) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
product_num--;
printf("consumer: the num of product is %d\n", product_num);
pthread_mutex_unlock(&mutex);
sem_post(&empty);
usleep(5000000);
printf("consume an item\n");
}
}關鍵陷阱:對 empty 和 full 的 P 操作必須放在對 mutex 的 P 操作之前。若生產者先 P(mutex) 再 P(empty),當 empty = 0 時生產者被阻塞在 P(empty),而它已經佔住 mutex;消費者此時想 P(mutex) 也會被阻塞——雙方互等對方釋放,形成死鎖。消費者先把緩衝區取空(full = 0)時同理。不過釋放信號量時,mutex 與 full 的的先後順序無所謂。
問題描述:兩個或以上讀線程同時讀共享資料不會出問題,但寫線程與任何其他線程同時訪問則可能導致資料不一致。要求:多讀者可同時讀;只允許一個寫者寫;寫者在完成前不允許其他讀者或寫者工作;寫者執行前應讓已有讀者與寫者全部退出。
分析:讀者與寫者互斥、寫者與寫者互斥,讀者之間不互斥。寫者簡單,一對 PV 即可;讀者需要在與寫者互斥的同時實現讀者之間的同步,因此引入計數器 count 判斷當前是否有讀者,並用 mutex 保護計數器。
sem_t rw; //保護讀者和寫者互斥訪問文件,初始化為 1
pthread_mutex_t mutex; //保護更新 reader_num 時的互斥
int reader_num = 0; //記錄讀者數量
void *writer_f(void *arg)
{
sem_wait(&rw);
usleep(2000000);
printf("writing ...\n");
sem_post(&rw);
}
void *reader_f(void *arg)
{
pthread_mutex_lock(&mutex);
if (reader_num == 0) sem_wait(&rw); //阻止寫線程寫
reader_num++;
printf("reader num add the num is %d \n", reader_num);
pthread_mutex_unlock(&mutex);
usleep(2000000);
printf("reading ...\n");
pthread_mutex_lock(&mutex);
reader_num--;
printf("reader num dec the num is %d \n", reader_num);
if (reader_num == 0) sem_post(&rw); //允許寫線程寫
pthread_mutex_unlock(&mutex);
}上述版本是讀者優先:只要還有讀者活躍,後續讀者都能進入,寫者可能長期等待甚至「餓死」。若希望寫者優先,只需增加一個信號量 w,並在兩函數中各加一對 PV:
sem_t rw; //讀寫互斥,初始化為 1
sem_t w; //實現寫優先,初始化為 1
pthread_mutex_t mutex;
int reader_num = 0;
void *writer_f(void *arg)
{
sem_wait(&w);
sem_wait(&rw);
usleep(2000000);
printf("writing ...\n");
sem_post(&rw);
sem_post(&w);
}
void *reader_f(void *arg)
{
sem_wait(&w);
pthread_mutex_lock(&mutex);
if (reader_num == 0) sem_wait(&rw);
reader_num++;
printf("reader num add the num is %d \n", reader_num);
pthread_mutex_unlock(&mutex);
sem_post(&w);
usleep(2000000);
printf("reading ...\n");
pthread_mutex_lock(&mutex);
reader_num--;
printf("reader num dec the num is %d \n", reader_num);
if (reader_num == 0) sem_post(&rw);
pthread_mutex_unlock(&mutex);
}問題描述:圓桌上坐著 5 名哲學家,每兩人之間放一根筷子,桌子中間是一碗飯。哲學家思考時不影響他人,飢餓時才試圖拿起左右兩根筷子(一根一根拿),若筷子在他人手上則等待,只有同時拿到兩根才能進餐,吃完放下繼續思考。
分析:5 名哲學家與左右鄰居對中間筷子的訪問是互斥關係。關鍵是避免死鎖與飢餓。常見解法有兩類:一是讓哲學家同時拿兩根筷子;二是制定規則,例如至多允許四人同時進餐、兩根筷子都可用時才允許拿、或奇偶號哲學家取筷順序相反。
信號量設置:chopstick[5] 初始化為 1,用於 5 根筷子的互斥訪問;哲學家 i 左邊筷子編號為 i,右邊為 (i+1)%5。
若不加限制,五人同時拿起左邊筷子後,再想拿右邊就全被阻塞,這正是死鎖。
sem_t chopstick[5]; //信號量數組,初始化為 1
pthread_mutex_t mutex; //保護取筷子時的互斥
void *philosopher_i(void *arg)
{
int index = *(int *)arg;
pthread_mutex_lock(&mutex); //取筷子前獲得互斥量
sem_wait(&chopstick[index]); //取左邊筷子
sem_wait(&chopstick[(index+1) % 5]); //取右邊筷子
printf("get two chopstick the index is %d\n", index);
pthread_mutex_unlock(&mutex);
usleep(2000000);
printf("eating... the index is %d\n", index);
sem_post(&chopstick[index]); //放回左邊筷子
sem_post(&chopstick[(index+1) % 5]); //放回右邊筷子
usleep(2000000);
printf("thinking... the index is %d\n", index);
}這裡的互斥量把「拿兩根筷子」變成一個不可打斷的動作,代價是並行度下降,但徹底排除了死鎖。
問題描述:系統有三個抽煙者線程和一個供應者線程。捲一支煙需要煙草、紙、膠水三種材料:第一個抽煙者擁有煙草,第二個擁有紙,第三個擁有膠水。供應者每次把兩種材料放到桌上,擁有剩下那種材料的抽煙者捲煙並抽掉,然後給供應者一個信號,供應者再放另外兩種材料,如此輪流進行。
分析:供應者與三個抽煙者分別是同步關係;供應者無法同時滿足兩個以上抽煙者,抽煙動作之間互斥。
信號量設置:offer1/offer2/offer3 分別代表「煙草+紙」「煙草+膠水」「紙+膠水」組合的資源,finish 用於互斥進行抽煙動作。
sem_t offer1; //煙草和紙的組合,初始化為 1
sem_t offer2; //煙草和膠水的組合
sem_t offer3; //紙和膠水的組合
sem_t finish; //抽煙者是否完成
void *provider_f(void *arg)
{
while (1) {
int rand_num = rand() % 3;
if (rand_num == 0) sem_post(&offer1);
else if (rand_num == 1) sem_post(&offer2);
else sem_post(&offer3);
usleep(1000000);
printf("put two material on table\n");
sem_wait(&finish);
}
}
void *smoker1_f(void *arg) //擁有煙草,需要紙和膠水
{
while (1) {
sem_wait(&offer1);
usleep(1000000);
printf("remove the paper and glue\n");
sem_post(&finish);
}
}
void *smoker2_f(void *arg) //擁有紙,需要煙草和膠水
{
while (1) {
sem_wait(&offer2);
usleep(1000000);
printf("remove the tobacco and glue\n");
sem_post(&finish);
}
}
void *smoker3_f(void *arg) //擁有膠水,需要紙和煙草
{
while (1) {
sem_wait(&offer3);
usleep(1000000);
printf("remove the paper and tobacco\n");
sem_post(&finish);
}
}參考資料:



https://c.biancheng.net/cpp/html/2600.html