小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

pthread_cond_signal和pthread_cond_wait簡(jiǎn)介

 mrjbydd 2012-08-26

pthread_cond_signal和pthread_cond_wait簡(jiǎn)介

原文: 
http://apps.hi.baidu.com/share/detail/19786281
http://topic.csdn.net/u/20110105/16/12717238-9816-4571-a03d-e8b603724946.html 
   pthread_cond_wait() 用于阻塞當(dāng)前線程,等待別的線程使用pthread_cond_signal()pthread_cond_broadcast來(lái)喚醒它。 pthread_cond_wait() 必須與pthread_mutex 配套使用。pthread_cond_wait()函數(shù)一進(jìn)入wait狀態(tài)就會(huì)自動(dòng)release mutex。當(dāng)其他線程通過(guò)pthread_cond_signal()pthread_cond_broadcast,把該線程喚醒,使pthread_cond_wait()通過(guò)(返回)時(shí),該線程又自動(dòng)獲得該mutex。
  pthread_cond_signal函數(shù)的作用是發(fā)送一個(gè)信號(hào)給另外一個(gè)正在處于阻塞等待狀態(tài)的線程,使其脫離阻塞狀態(tài),繼續(xù)執(zhí)行.如果沒(méi)有線程處在阻塞等待狀態(tài),pthread_cond_signal也會(huì)成功返回。
  使用pthread_cond_signal一般不會(huì)有“驚群現(xiàn)象”產(chǎn)生,他最多只給一個(gè)線程發(fā)信號(hào)。假如有多個(gè)線程正在阻塞等待著這個(gè)條件變量的話,那么是根據(jù)各等待線程優(yōu)先級(jí)的高低確定哪個(gè)線程接收到信號(hào)開(kāi)始繼續(xù)執(zhí)行。如果各線程優(yōu)先級(jí)相同,則根據(jù)等待時(shí)間的長(zhǎng)短來(lái)確定哪個(gè)線程獲得信號(hào)。但無(wú)論如何一個(gè)pthread_cond_signal調(diào)用最多發(fā)信一次。
  但是pthread_cond_signal在多處理器上可能同時(shí)喚醒多個(gè)線程,當(dāng)你只能讓一個(gè)線程處理某個(gè)任務(wù)時(shí),其它被喚醒的線程就需要繼續(xù) wait,而且規(guī)范要求pthread_cond_signal至少喚醒一個(gè)pthread_cond_wait上的線程,其實(shí)有些實(shí)現(xiàn)為了簡(jiǎn)單在單處理器上也會(huì)喚醒多個(gè)線程. 
   另外,某些應(yīng)用,如線程池,pthread_cond_broadcast喚醒全部線程,但我們通常只需要一部分線程去做執(zhí)行任務(wù),所以其它的線程需要繼續(xù)wait.所以強(qiáng)烈推薦對(duì)pthread_cond_wait() 使用while循環(huán)來(lái)做條件判斷.
以下就是一個(gè)來(lái)自MAN的示例
  Consider two shared variables x and y, protected by the mutex mut, and a condition vari-
       able cond that is to be signaled whenever x becomes greater than y.

              int x,y;
              pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
              pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

       Waiting until x is greater than y is performed as follows:

              pthread_mutex_lock(&mut);
              while (x <= y) {
                      pthread_cond_wait(&cond, &mut);
              }
              /* operate on x and y */
              pthread_mutex_unlock(&mut);

       Modifications on x and y that may cause x to become greater than y should signal the con-
       dition if needed:
              pthread_mutex_lock(&mut);
              /* modify x and y */
              if (x > y) pthread_cond_broadcast(&cond);
              pthread_mutex_unlock(&mut);


pthread_cond_signal函數(shù)與條件變量的典型應(yīng)用就是用來(lái)實(shí)現(xiàn)producer/consumer模型。
示例1

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define BUFFER_SIZE 8
struct Products
{
int buffer[BUFFER_SIZE];
/*保證存取操作的原子性 互斥性*/
pthread_mutex_t locker;
/*是否可讀*/           
pthread_cond_t notEmpty;
/*是否可寫(xiě)*/  
pthread_cond_t notFull;
int posReadFrom;
int posWriteTo;
};
int BufferIsFull(struct Products* products)
{
if ((products->posWriteTo + 1) % BUFFER_SIZE == products->posReadFrom)
{
return (1);
}
return (0);
}
int BufferIsEmpty(struct Products* products)
{
if (products->posWriteTo == products->posReadFrom)
{
return (1);
}
return (0);
}
/*制造產(chǎn)品*/。
void Produce(struct Products* products, int item)
{
/*原子操作*/
pthread_mutex_lock(&products->locker);
/*無(wú)空間可寫(xiě)入*/
while (BufferIsFull(products))
{
pthread_cond_wait(&products->notFull, &products->locker);
} 
/*寫(xiě)入數(shù)據(jù)*/
products->buffer[products->posWriteTo] = item;
products->posWriteTo++;
if (products->posWriteTo >= BUFFER_SIZE)
products->posWriteTo = 0;
/*發(fā)信*/
pthread_cond_signal(&products->notEmpty);
/*解鎖*/
pthread_mutex_unlock(&products->locker);
}
int Consume(struct Products* products)
{
int item;
pthread_mutex_lock(&products->locker);
/*為空時(shí)持續(xù)等待,無(wú)數(shù)據(jù)可讀*/
while (BufferIsEmpty(products))
{
pthread_cond_wait(&products->notEmpty, &products->locker);
}
/*提取數(shù)據(jù)*/
item = products->buffer[products->posReadFrom];
products->posReadFrom++;
/*如果到末尾,從頭讀取*/
if (products->posReadFrom >= BUFFER_SIZE)
products->posReadFrom = 0;
pthread_cond_signal(&products->notFull); 
pthread_mutex_unlock(&products->locker);
return item;
}
#define END_FLAG (-1)
struct Products products;
void* ProducerThread(void* data)
{
int i;
for (i = 0; i < 16; ++i)
{
printf("producer: %d\n", i);
Produce(&products, i);
}
Produce(&products, END_FLAG);
return NULL;
}
void* ConsumerThread(void* data)
{
int item;
while (1)
{
item = Consume(&products);
if (END_FLAG == item)
       break;
printf("consumer: %d\n", item);
}
return (NULL);
}
int main(int argc, char* argv[])
{
pthread_t producer;
pthread_t consumer;
int result;
pthread_create(&producer, NULL, &ProducerThread, NULL);
pthread_create(&consumer, NULL, &ConsumerThread, NULL);
pthread_join(producer, (void *)&result);
pthread_join(consumer, (void *)&result);
exit(EXIT_SUCCESS);
}

示例2
pthread_cond_broadcast的是使用

pthread_mutex_t mymutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mymutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t mycond = PTHREAD_COND_INITIALIZER;
 void *mythread1(void *param)
 {
pthread_mutex_lock(&mymutex1);
pthread_cond_wait(&mycond,&mymutex1);
fprintf(stderr,"this is mythread1.\n");
pthread_mutex_unlock(&mymutex1);
return NULL;
 }
 void *mythread2(void *param)
 {
pthread_mutex_lock(&mymutex2);
pthread_cond_wait(&mycond,&mymutex2);
fprintf(stderr,"this is mythread2.\n");
pthread_mutex_unlock(&mymutex2);
return NULL;
 }
 int main(int argc,char* argv[],char *envp[])
 {
int i;
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,mythread1,NULL);
pthread_create(&tid2,NULL,mythread2,NULL);
sleep(2)
if(pthread_cond_broadcast(&mycond)){
printf("error\n");
return 1;
  }
  void *res;
  pthread_join(tid1, &res);
  pthread_join(tid2, &res);
printf("this is main thread.\n");
return 0;
 }

結(jié)束!

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多