日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

3线程同步:条件变量

發(fā)布時間:2024/9/27 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 3线程同步:条件变量 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。