3线程同步:条件变量
1條件變量
條件變量給多個線程提供了一個匯合的場所。
依賴的頭文件
#include<pthread.h>
函數(shù)聲明
定義分配條件變量
pthread_cond_t cond =PTHREAD_COND_INITIALIZER;
?
int pthread_cond_init(pthread_cond_t*restrict cond, const pthread_condattr_t *restrict attr);
| 名稱: | pthread_cond_init |
| 功能: | initialize condition variables??初始化條件變量 |
| 頭文件: | #include <pthread.h> |
| 函數(shù)原形: | int pthread_cond_init(pthread_cond_t *restrict cond, ?????????????const pthread_condattr_t *restrict attr); |
| 參數(shù): | ? |
| 返回值: | the pthread_cond_destroy() and pthread_cond_init() functions?shall?return zero; otherwise, an error number shall be returned to indicate the error |
?
int pthread_cond_destroy(pthread_cond_t*cond);
| 名稱: | pthread_cond_destroy |
| 功能: | Destroy condition variables? 銷毀條件變量 |
| 頭文件: | #include <pthread.h> |
| 函數(shù)原形: | int pthread_cond_destroy(pthread_cond_t *cond); |
| 參數(shù): | ? |
| 函數(shù)說明: | 在釋放或廢棄條件變量之前,需要?dú)乃?#xff0c;使用此函數(shù) |
| 返回值: | the pthread_cond_destroy() and pthread_cond_init() functions?shall?return zero; otherwise, an error number shall be returned to indicate the error |
?
int pthread_cond_wait(pthread_cond_t*restrict cond,pthread_mutex_t *restrict mutex);
| 名稱: | pthread_cond_wait |
| 功能: | Wait on a condition,等待某個條件是否成立。對于timewait()函數(shù)除了等待以外,可以設(shè)置一個時長。 |
| 頭文件: | #include <pthread.h> |
| 函數(shù)原形: | intpthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex); |
| 參數(shù): | ? |
| 函數(shù)說明: | 一旦初始化了互斥對象和條件變量,就可以等待這個條件,一個特定條件只能有一個互斥對象,而且條件變量應(yīng)該表示互斥數(shù)據(jù)“內(nèi)部”的一種特殊的條件更改。一個互斥條件可以用許多條件變量(例如:cond_empty,cond_full,cond_cleanup),但每個條件變量只能有一個互斥對象。 |
| 返回值: | ? |
?
int pthread_cond_signal(pthread_cond_t*cond);
| 名稱: | pthread_cond_signal |
| 功能: | signal a?condition,這種情況是只有一個線程收到后執(zhí)行動作。 |
| 頭文件: | #include <pthread.h> |
| 函數(shù)原形: | int pthread_cond_signal(pthread_cond_t *cond); |
| 參數(shù): | ? |
| 函數(shù)說明: | 活動線程只需要喚醒第一個正在睡眠的線程。假設(shè)您只對隊(duì)列添加了一個工作作業(yè)。那么只需要喚醒一個工作程序線程(再喚醒其它線程是不禮貌的!) |
| 返回值: | ? |
?
int pthread_cond_broadcast(pthread_cond_t*cond);
| 名稱: | pthread_cond_broadcast |
| 功能: | broadcast a?condition,通過廣播的形式發(fā)給子線程消息,子線程競爭執(zhí)行。 |
| 頭文件: | #include <pthread.h> |
| 函數(shù)原形: | int pthread_cond_broadcast(pthread_cond_t *cond); |
| 參數(shù): | ? |
| 函數(shù)說明: | 如果線程更改某些共享數(shù)據(jù),而且它想要喚醒所有正在等待的線程,則應(yīng)使用 pthread_cond_broadcast調(diào)用 |
| 返回值: | ? |
案例說明:
| #include <stdlib.h> #include <pthread.h> #include <stdio.h> #include <unistd.h> ? struct msg { ???struct msg *next; ???int num; }; ? struct msg *head; /*條件變量 */ pthread_cond_t has_product = PTHREAD_COND_INITIALIZER; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; void *consumer(void *p) { ???struct msg *mp; ???for (;;) { ???????pthread_mutex_lock(&lock); ???????/* pthread_cond_wait(&has_product, &lock); ????????* 1.阻塞等待has_product被喚醒, ????????* 2.釋放互斥鎖, pthread_mutex_unlock(&lock) ????????* 3.當(dāng)被喚醒時,解除阻塞,并且重新去申請獲得互斥鎖 pthread_mutex_lock(&lock) ????????*/ ???????while (head == NULL) ???????????pthread_cond_wait(&has_product, &lock); ? ???????mp = head; ???????head = mp->next; ???????pthread_mutex_unlock(&lock); ???????printf("Consume %d\n", mp->num); ???????free(mp); ???????sleep(rand() % 5); ???} } ? void *producer(void *p) { ???struct msg *mp; ???for (;;) { ???????mp =?(struct msg *)malloc(sizeof(struct msg)); ???????mp->num = rand() % 1000 + 1; ???????printf("Produce %d\n", mp->num); ???????pthread_mutex_lock(&lock); ???????mp->next = head; ???????head = mp; ???????pthread_mutex_unlock(&lock); ???????/* pthread_cond_broadcast(&has_product)喚醒等待隊(duì)列上的所有線程*/ ??????????????????//發(fā)送信號,告訴消費(fèi)者有產(chǎn)品了 ???????pthread_cond_signal(&has_product); ???????sleep(rand() % 5); ???} } ? int main(int argc, char *argv[]) { ???pthread_t pid, cid; ???srand(time(NULL)); ???pthread_create(&pid, NULL, producer, NULL); ???pthread_create(&cid, NULL, consumer, NULL); ???pthread_join(pid, NULL); ???pthread_join(cid, NULL); ???return 0; } |
運(yùn)行結(jié)果:
總結(jié):從上面可以看出,消費(fèi)者總是消費(fèi)最先生產(chǎn)出來的一個。
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的3线程同步:条件变量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 台式电脑的音量的红叉怎么 台式电脑音量红
- 下一篇: 4线程同步:信号量