linux - 如何理解互斥鎖和條件變量?
問題描述
下面的代碼出自《Unix/Linux編程實踐教程》,作用是用兩個線程分別統(tǒng)計兩個文件的單詞的數(shù)目,并在主線程中計算總數(shù)。下面是運行截圖:
但是看了半天還是難以理解下面代碼中的加鎖、解鎖以及條件變量。我想問:
主線程中調(diào)用pthread_cond_wait時會釋放互斥鎖,然后掛起主線程,并等待條件變量的發(fā)生變化,當其他線程調(diào)用pthread_cond_signal時,如果互斥鎖是被鎖住的,那么主線程中的pthread_cond_wait會等互斥待鎖被解鎖后,然后再給互斥鎖上鎖后再返回嗎?
如果正如1中的描述那樣的話,pthread_cond_wait收到了pthread_cond_signal發(fā)來的信號,但是未鎖定互斥鎖之前,又被其他線程搶了先,鎖住了互斥鎖,那不是pthread_cond_wait還得掛起等待互斥鎖被解鎖?
如果可以的話,希望能幫助理清一下這個程序的執(zhí)行流程。
萬分感謝。
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <ctype.h>struct arg_set { /* two values int one arg */ char *filename; /* file to examine */ int count; /* number of words */ int code;};struct arg_set *mailbox = NULL;pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t flag = PTHREAD_COND_INITIALIZER;void *count_words(void *);int main(int argc, char *argv[]){ pthread_t t1, t2; /* two threads */ struct arg_set args1, args2; /* two argsets */ int reports_int = 0; int total_words = 0; if (argc != 3) {fprintf(stderr, 'usage: %s file1 file2', argv[0]);exit(1); } pthread_mutex_lock(&lock); args1.filename = argv[1]; args1.count = 0; args1.code = 1; pthread_create(&t1, NULL, count_words, (void *)&args1); args2.filename = argv[2]; args2.count = 0; args2.code = 2; pthread_create(&t2, NULL, count_words, (void *)&args2); while (reports_int < 2) { // 等待其他線程結(jié)束printf('MAIN: waiting for flag to go upn');pthread_cond_wait(&flag, &lock);printf('MAIN: Wow! flag was raised, I have the lockn');printf('%7d: %sn', mailbox->count, mailbox->filename);total_words += mailbox->count;if (mailbox == &args1) pthread_join(t1, NULL);if (mailbox == &args2) pthread_join(t2, NULL);mailbox = NULL;pthread_cond_signal(&flag);reports_int++; } printf('%7d: total wordsn', total_words); return 0;}void *count_words(void *a){ struct arg_set *args = a; FILE *fp; int c, prevc = ’0’; if ((fp = fopen(args->filename, 'r')) != NULL) { // 統(tǒng)計單詞個數(shù)while ((c = getc(fp)) != EOF) { if (!isalnum(c) && isalnum(prevc))args->count++; prevc = c;}fclose(fp); } elseperror(args->filename); printf('COUNT %d: waiting to get lockn', args->code); pthread_mutex_lock(&lock); printf('COUNT %d: have lock, storing datan', args->code); if (mailbox != NULL)pthread_cond_wait(&flag, &lock); mailbox = args; printf('COUNT %d: raising flagn', args->code); pthread_cond_signal(&flag); printf('COUNT %d: unlocking boxn', args->code); pthread_mutex_unlock(&lock); return NULL;}
問題解答
回答1:沒什么復雜的,主線程獲得鎖后,進入休眠,等一個信號來喚醒它。
pthread_cond_signal就是這個信號
這種鎖與別的鎖有點不同,其它類型的鎖是:線程申請鎖,沒有得到鎖,線程就進入休眠,等待。
這種鎖是有鎖就休眠,等別的線程叫醒它。
相關(guān)文章:
1. docker gitlab 如何git clone?2. docker鏡像push報錯3. 關(guān)于docker下的nginx壓力測試4. 在windows下安裝docker Toolbox 啟動Docker Quickstart Terminal 失敗!5. angular.js使用$resource服務(wù)把數(shù)據(jù)存入mongodb的問題。6. javascript - 怎么看網(wǎng)站用了什么技術(shù)框架?7. PC 手機兼容的 編輯器8. android - 項目時間長了,字符串文件strings有的字符串可能是多余的,有沒有辦法快速檢測那些是沒用的?9. angular.js - angularJs ngRoute怎么在路由傳遞空字符串及用ng-switch取得10. mysql - 用PHPEXCEL將excel文件導入數(shù)據(jù)庫數(shù)據(jù)5000+條,本地數(shù)據(jù)庫正常,線上只導入15條,沒有報錯,哪里的問題?
