Der folgende Code stammt aus dem ?Unix/Linux Programming Practice Tutorial“. Seine Funktion besteht darin, zwei Threads zu verwenden, um jeweils die Anzahl der W?rter in zwei Dateien zu z?hlen und die Gesamtzahl im Hauptthread zu berechnen. Unten sehen Sie einen Screenshot des Vorgangs:
Aber nach l?ngerem Lesen ist es immer noch schwierig, die Sperr-, Entsperr- und Bedingungsvariablen im folgenden Code zu verstehen.
Ich m?chte fragen:
Wird beim Aufrufen von pthread_cond_wait
時會釋放互斥鎖,然后掛起主線程,并等待條件變量的發(fā)生變化,當其他線程調(diào)用pthread_cond_signal
時,如果互斥鎖是被鎖住的,那么主線程中的pthread_cond_wait
im Hauptthread darauf gewartet, dass der Mutex entsperrt wird, und dann der Mutex gesperrt, bevor zurückgekehrt wird?
Wenn es wie in 1 beschrieben ist, pthread_cond_wait
收到了pthread_cond_signal
發(fā)來的信號,但是未鎖定互斥鎖之前,又被其他線程搶了先,鎖住了互斥鎖,那不是pthread_cond_wait
müssen Sie immer noch warten, bis die Mutex-Sperre entsperrt wird?
Wenn m?glich, hoffe ich, dass es dabei helfen kann, den Ausführungsprozess dieses Programms zu kl?ren.
Vielen Dank.
#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 up\n");
pthread_cond_wait(&flag, &lock);
printf("MAIN: Wow! flag was raised, I have the lock\n");
printf("%7d: %s\n", 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 words\n", total_words);
return 0;
}
void *count_words(void *a)
{
struct arg_set *args = a;
FILE *fp;
int c, prevc = 'Rrreee';
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);
} else
perror(args->filename);
printf("COUNT %d: waiting to get lock\n", args->code);
pthread_mutex_lock(&lock);
printf("COUNT %d: have lock, storing data\n", args->code);
if (mailbox != NULL)
pthread_cond_wait(&flag, &lock);
mailbox = args;
printf("COUNT %d: raising flag\n", args->code);
pthread_cond_signal(&flag);
printf("COUNT %d: unlocking box\n", args->code);
pthread_mutex_unlock(&lock);
return NULL;
}
沒什么復雜的,主線程獲得鎖后,進入休眠,等一個信號來喚醒它。
pthread_cond_signal
就是這個信號
這種鎖與別的鎖有點不同,其它類型的鎖是:線程申請鎖,沒有得到鎖,線程就進入休眠,等待。
這種鎖是有鎖就休眠,等別的線程叫醒它。