一江春水向東流

          做一個(gè)有思想的人,期待與每一位熱愛思考的人交流,您的關(guān)注是對(duì)我最大的支持。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            44 隨筆 :: 139 文章 :: 81 評(píng)論 :: 0 Trackbacks

          一個(gè)很好的多線程和互斥鎖學(xué)習(xí)例程,是一個(gè)生產(chǎn)者-消費(fèi)者的模型


          #include <stdio.h>
          #include <stdlib.h>
          #include <time.h>
          #include <pthread.h>
          #define BUFFER_SIZE 16

          struct prodcons
          {
          ?? int buffer[BUFFER_SIZE];
          ?? pthread_mutex_t lock;/*互斥鎖*/
          ?? int readpos, writepos;
          ?? pthread_cond_t notempty;/*緩沖區(qū)非空信號(hào)*/
          ?? pthread_cond_t notfull;/*緩沖區(qū)非滿信號(hào)*/
          };

          void init(struct prodcons *b)
          {
          ?? pthread_mutex_init(&b->lock, NULL);
          ?? pthread_cond_init(&b->notempty, NULL);
          ?? pthread_cond_init(&b->notfull, NULL);
          ?? b->readpos = 0;
          ?? b->writepos = 0;
          }
          void put(struct prodcons *b, int data)
          {
          ?? pthread_mutex_lock(&b->lock);//獲取互斥鎖
          ?? while((b->writepos + 1) % BUFFER_SIZE == b->readpos)
          ?? {
          ????? printf("wait for not full\n");
          ????? pthread_cond_wait(&b->notfull, &b->lock);//不滿時(shí)逃出阻塞
          ?? }
          ?? b->buffer[b->writepos] = data;
          ?? b->writepos++;
          ?? if(b->writepos >= BUFFER_SIZE) b->writepos = 0;
          ?? pthread_cond_signal(&b->notempty);//設(shè)置狀態(tài)變量
          ?? pthread_mutex_unlock(&b->lock);//釋放互斥鎖
          }

          int get(struct prodcons *b)
          {
          ?? int data;
          ?? pthread_mutex_lock(&b->lock);
          ?? while(b->writepos == b->readpos)
          ?? {
          ???? printf("wait for not empty\n");
          ???? pthread_cond_wait(&b->notempty, &b->lock);
          ?? }
          ?? data = b->buffer[b->readpos];
          ?? b->readpos++;
          ?? if(b->readpos >= BUFFER_SIZE) b->readpos = 0;
          ?? pthread_cond_signal(&b->notfull);
          ?? pthread_mutex_unlock(&b->lock);
          ?? return data;
          }

          #define OVER (-1)
          struct prodcons buffer;

          void *producer(void *data)
          {
          ?? int n;
          ?? for(n=0; n<1000; n++)
          ?? {
          ????? printf("put->%d\n",n);
          ????? put(&buffer, n);
          ?? }
          ?? put(&buffer, OVER);
          ?? printf("producer stopped\n");
          ?? return NULL;
          }

          void *consumer(void *data)
          {
          ?? int d;
          ?? while(1)
          ?? {
          ????? d = get(&buffer);
          ????? if(d == OVER)
          ????? break;
          ????? printf("???????????? %d->get\n",d);
          ?? }
          ?? printf("consumer stopped!\n");
          ?? return NULL;
          }
          int main(void)
          {
          ?? pthread_t th_a, th_b;
          ?? void *retval;
          ?? init(&buffer);
          ?? pthread_create(&th_a, NULL, producer, 0);
          ?? pthread_create(&th_b, NULL, consumer, 0);
          ?? pthread_join(th_a, &retval);
          ?? pthread_join(th_b, &retval);
          ?? return 0;
          }
          pthread_cond_wait函數(shù),是線程阻塞在一個(gè)條件變量上,原型為:
          extern int pthread_cond_wait(pthread_cond_t *_restrict_cond, pthread_mutex_t* _restrict_mutex)
          線程解開mutex指向的鎖并被條件變量cond阻塞,線程可以被函數(shù)pthread_cond_signal和pthread_cond_broadcast喚醒。
          還有另一個(gè)函數(shù)pthread_cond_timedwait函數(shù),他比vpthread_cond_wait函數(shù)多一個(gè)時(shí)間參數(shù),經(jīng)歷給定時(shí)間後,阻塞將被解除。

          posted on 2007-05-03 13:44 allic 閱讀(727) 評(píng)論(0)  編輯  收藏 所屬分類: C/C++
          主站蜘蛛池模板: 台东市| 麻阳| 长岭县| 理塘县| 裕民县| 清远市| 桂阳县| 茌平县| 林芝县| 团风县| 贵定县| 汕头市| 邢台市| 萨嘎县| 德州市| 民丰县| 信宜市| 阿克| 灵川县| 丰台区| 永寿县| 昭平县| 乌拉特前旗| 仁怀市| 秭归县| 略阳县| 金秀| 东乌珠穆沁旗| 亳州市| 惠东县| 永兴县| 中江县| 砚山县| 玉田县| 精河县| 望谟县| 巨野县| 扶余县| 聂荣县| 嵊州市| 高台县|