“使用pthread_barrIEr_init和pthread_barrIEr_wait确保所有生产者/消费者线程同时开始生产/消费.”
顺便说一下,这是作业的额外学分
#include <pthread.h>#include <stdlib.h>#include <unistd.h>#include <stdio.h>#define SIXTY_SECONDS 60000000#define ONE_SECOND 1000000#define RANGE 10#define PERIOD 2typedef struct { int *carpark; int capacity; int occupIEd; int nextin; int nextout; int cars_in; int cars_out; pthread_mutex_t lock; pthread_cond_t space; pthread_cond_t car; pthread_barrIEr_t bar;} cp_t;/* Our producer threads will each execute this function */static voID *producer(voID *cp_in){ cp_t *cp; unsigned int seed; /* Convert what was passed in to a pointer to a bounded buffer */ cp = (cp_t *)cp_in; /* Loop */ while (1) { /* Sleep for up to 1s */ usleep(rand_r(&seed) % ONE_SECOND); /* Acquire the lock */ pthread_mutex_lock(&cp->lock); /* While full wait until there is room available */ while (cp->occupIEd == cp->capacity) { pthread_cond_wait(&cp->car,&cp->lock); } /* Insert an item */ cp->carpark[cp->nextin] = rand_r(&seed) % RANGE; /* Increment counters */ cp->occupIEd++; cp->nextin++; cp->nextin %= cp->capacity; cp->cars_in++; /* Someone may be waiting on data to become available */ pthread_cond_signal(&cp->space); /* Release the lock */ pthread_mutex_unlock(&cp->lock); } return ((voID *)NulL);}/* Our consumer threads will each execute this function */static voID *consumer(voID *cp_in){ cp_t *cp; unsigned int seed; /* Convert what was passed in to a pointer to a bounded buffer */ cp = (cp_t *)cp_in; while (1) { /* Sleep for up to 1s */ usleep(rand_r(&seed) % ONE_SECOND); /* Acquire the lock */ pthread_mutex_lock(&cp->lock); /* While empty wait until there is data available */ while (cp->occupIEd == 0) { pthread_cond_wait(&cp->space,&cp->lock); } /* Increment counters */ cp->occupIEd--; cp->nextout++; cp->nextout %= cp->capacity; cp->cars_out++; /* Someone may be waiting on room to become available */ pthread_cond_signal(&cp->car); /* Release the lock */ pthread_mutex_unlock(&cp->lock); } return ((voID *)NulL);}/* Our monitor thread will each execute this function */static voID *monitor(voID *cp_in){ cp_t *cp; /* Convert what was passed in to a pointer to a bounded buffer */ cp = (cp_t *)cp_in; while (1) { /* Pause */ sleep(PERIOD); /* Acquire the lock */ pthread_mutex_lock(&cp->lock); printf("Delta: %d\n",cp->cars_in - cp->cars_out); /* Release the lock */ pthread_mutex_unlock(&cp->lock); } return ((voID *)NulL);}/* Initialisation */static intinit(cp_t *cp,int capacity){ /* Set up the bounded buffer internals */ cp->occupIEd = cp->nextin = cp->nextout = cp->cars_in = cp->cars_out = 0; cp->capacity = capacity; /* Initialise our data structure */ cp->carpark = (int *)malloc(cp->capacity * sizeof (*cp->carpark)); /* Check malloc succeeded */ if (cp->carpark == NulL) { perror("malloc()"); exit(EXIT_FAILURE); } /* Initialise lock and condition variables */ pthread_mutex_init(&cp->lock,NulL); pthread_cond_init(&cp->space,NulL); pthread_cond_init(&cp->car,NulL); /* Seed random number generator */ srand((unsigned int)getpID()); return (0);}intmain(int argc,char *argv[]){ pthread_t p,c,m; cp_t cp; /* Check usage */ if (argc != 2) { printf("Usage: %s buffer_size\n",argv[0]); exit(EXIT_FAILURE); } /* Initialise */ init(&cp,atoi(argv[1])); /* Create our threads */ pthread_create(&p,NulL,producer,(voID *)&cp); pthread_create(&p,(voID *)&cp); pthread_create(&c,consumer,(voID *)&cp); pthread_create(&m,monitor,(voID *)&cp); /* Wait for our threads */ pthread_join(p,NulL); pthread_join(p,NulL); pthread_join(c,NulL); pthread_join(m,NulL); return (0);}解决方法 我可能会给你完整的答案,但我害怕Lasse V. Karlsen.所以我会给你提示.
>您的struct cp_t中已经可以访问屏障对象栏
>使用pthread_barrIEr_init初始化它,就像初始化互斥锁一样.计数与演员数量之间存在对应关系.
>在开始生产/消费之前,生产者和消费者都需要wait
.得到它 ?
以上是内存溢出为你收集整理的c – 如何使用pthreads屏障?全部内容,希望文章能够帮你解决c – 如何使用pthreads屏障?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)