内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

[Linux線程]線程的同步--使用條件變量完成線程同步

#include <stdio.h>
#include <pthread.h>
#define BUFFER_SIZE 4
#define OVER (-1)
struct producers                                        //定義生產(chǎn)者條件變量結(jié)構(gòu)。
{
        int buffer[BUFFER_SIZE];                        //定義緩沖區(qū)。
        pthread_mutex_t lock;                           //定義訪問緩沖區(qū)的互斥鎖。
        int             readpos, writepos;                      //讀寫的位置。
        pthread_cond_t  notempty;                       //緩沖區(qū)有數(shù)據(jù)時(shí)的標(biāo)記。
        pthread_cond_t  notfull;                        //緩沖區(qū)未滿的標(biāo)記。
};
//初始化緩沖區(qū)
void init(struct producers *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;
}
//在緩沖區(qū)中存放一個(gè)整數(shù)。
void put(struct producers *b, int data)
{
        pthread_mutex_lock(&b->lock);
        //當(dāng)緩沖區(qū)為滿時(shí)等待。
        while((b->writepos+1)%BUFFER_SIZE == b->readpos)
        {
          pthread_cond_wait(&b->notfull,&b->lock);
          //在返回之前,pthread_cond_wait需要參數(shù)b->lock。
        }
        //向緩沖區(qū)中寫數(shù)據(jù),并將寫指針向前移動(dòng)。
        b->buffer[b->writepos] = data;
        b->writepos++;
        if(b->writepos >= BUFFER_SIZE)
        {
         b->writepos=0;
        }
        //發(fā)送當(dāng)前緩沖區(qū)中有數(shù)據(jù)的信號(hào)。
        pthread_cond_signal(&b->notempty);
        pthread_mutex_unlock(&b->lock);
}
//從緩沖區(qū)中讀數(shù)據(jù)并將數(shù)據(jù)從緩沖區(qū)中移走。
int get(struct producers *b)
{
        int data;
        pthread_mutex_lock(&b->lock);
        //當(dāng)緩沖區(qū)中有數(shù)據(jù)時(shí)等待。
        while(b->writepos == b->readpos)
        {
           pthread_cond_wait(&b->notempty,&b->lock);
        }
       //從緩沖區(qū)中讀數(shù)據(jù),并將指針前移。
       data = b->buffer[b->readpos];
       b->readpos++;
       if(b->readpos >= BUFFER_SIZE)
       {
         b->readpos = 0;
       }
       //發(fā)送當(dāng)前緩沖區(qū)未滿的信號(hào)。
       pthread_cond_signal(&b->notfull);
       pthread_mutex_unlock(&b->lock);
       return data;
}
struct producers  buffer;
//這是生產(chǎn)者的線程處理函數(shù)
void *producer(void *data)
{
    int n;
    for(n=0;n<10;n++)
    {
      printf("生產(chǎn)者: %d-->\n",n);  //連續(xù)10次生產(chǎn)
      put(&buffer,n);
    }
    put(&buffer,OVER);         //將狀態(tài)放入buffer中
    return NULL;
}
//這是消費(fèi)者的線程處理函數(shù)
void *consumer(void *data)
{
    int d;
    while(1)
    {
      d = get(&buffer);  //從buffer中讀取對(duì)應(yīng)的狀態(tài)
      if(d == OVER)      //如果已經(jīng)沒有了則停止
      {
       break;
      }
      printf("消費(fèi)者: --> %d\n",d);
    }
    return NULL;
}
//這是主程序
int main(int argc,char *argv[])
{
    pthread_t thproducer,thconsumer;    //生產(chǎn)者和消費(fèi)者的id
    void *retval;
    init(&buffer);    //初始化緩沖區(qū)
    pthread_create(&thproducer,NULL,producer,0); 
    pthread_create(&thconsumer,NULL,consumer,0);  //創(chuàng)建兩個(gè)線程
    pthread_join(thproducer,&retval);
    pthread_join(thconsumer,&retval);    //阻塞進(jìn)程
    return 0;
}

新聞名稱:[Linux線程]線程的同步--使用條件變量完成線程同步
標(biāo)題鏈接:http://www.rwnh.cn/article0/gcggio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、定制網(wǎng)站、App開發(fā)、手機(jī)網(wǎng)站建設(shè)網(wǎng)站導(dǎo)航、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)
会理县| 常宁市| 铁岭市| 元谋县| 双鸭山市| 融水| 普格县| 屏边| 东港市| 苏尼特右旗| 峨山| 榕江县| 大荔县| 大余县| 镇沅| 台前县| 稻城县| 布尔津县| 滕州市| 方城县| 平阴县| 涞水县| 蓬安县| 阿鲁科尔沁旗| 米脂县| 阜新| 色达县| 广饶县| 开远市| 北票市| 阳东县| 营口市| 瑞安市| 鄂托克旗| 清河县| 双辽市| 临夏市| 申扎县| 景泰县| 普陀区| 神木县|