如何使用pthreads的屏障? [英] How to use pthreads barrier?
问题描述
喜对不起张贴code的自卸大,但我在C $ C $Ç很新,基本上我做了一个大学生分配和,我必须实行pthread_barrier,现在我了解屏障的概念(或至少我认为我做的),但我只是不知道确切位置,我应该把它。分配状态:
使用pthread_barrier_init和pthread_barrier_wait,以确保所有的生产者/消费者线程开始生产/在同一时间消耗。
这是顺便分配额外的信贷部分
的#include< pthreads.h中>
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&stdio.h中GT;#定义SIXTY_SECONDS 6000
ONE_SECOND的#define百万
#定义范围10
#定义周期2typedef结构{
INT *停车场;
INT能力;
INT占用;
INT nextin;
INT nextout;
INT cars_in;
INT cars_out;
pthread_mutex_t锁;
pthread_cond_t的空间;
pthread_cond_t的车;
pthread_barrier_t吧;
} cp_t;/ *我们的生产线将分别执行此功能* /
静态无效*
制片人(无效* CP_IN)
{
cp_t * CP;
unsigned int类型的种子;
/ *转换到一个指针传递给一个界缓冲区什么* /
CP =(cp_t *)CP_IN; / *循环* /
而(1){
/ *睡眠多达1秒* /
usleep(rand_r(安培;种子)ONE_SECOND%);
/ *获取锁* /
调用pthread_mutex_lock(安培; CP-GT&;锁);
/ *当满等到有客房* /
而(CP-GT&;占用== CP-GT&;容量){
调用pthread_cond_wait(安培; CP-GT&;汽车,和放大器; CP-GT&;锁);
}
/ *插入一个项目* /
CP-GT&;停车场[CP-GT&; nextin] = rand_r(安培;种子)%范围;
/ *增量柜* /
CP-GT&;占用++;
CP-GT&; nextin ++;
CP-GT&; nextin%= CP-GT&;能力;
CP-GT&; cars_in ++;
/ *有人可能正在等待数据变得可用* /
调用pthread_cond_signal(安培; CP-GT&;空间);
/ *释放锁定* /
调用pthread_mutex_unlock(安培; CP-GT&;锁);
} 收益率((无效*)NULL);
}/ *我们的消费者线程将分别执行此功能* /
静态无效*
消费者(无效* CP_IN)
{ cp_t * CP;
unsigned int类型的种子;
/ *转换到一个指针传递给一个界缓冲区什么* /
CP =(cp_t *)CP_IN; 而(1){
/ *睡眠多达1秒* /
usleep(rand_r(安培;种子)ONE_SECOND%);
/ *获取锁* /
调用pthread_mutex_lock(安培; CP-GT&;锁);
/ *当空等待,直到有可用的数据* / 而(CP-GT&;占用== 0){
调用pthread_cond_wait(安培; CP-GT&;空间和放大器; CP-GT&;锁);
} / *增量柜* /
CP-GT&; occupied--;
CP-GT&; nextout ++;
CP-GT&; nextout%= CP-GT&;能力;
CP-GT&; cars_out ++;
/ *有人可能会等待空间变得可用* /
调用pthread_cond_signal(安培; CP-GT&;汽车);
/ *释放锁定* /
调用pthread_mutex_unlock(安培; CP-GT&;锁);
} 收益率((无效*)NULL);
}/ *我们的监视线程将分别执行此功能* /
静态无效*
监测(无效* CP_IN)
{ cp_t * CP;
/ *转换到一个指针传递给一个界缓冲区什么* /
CP =(cp_t *)CP_IN; 而(1){
/ *暂停* /
睡眠(PERIOD);
/ *获取锁* /
调用pthread_mutex_lock(安培; CP-GT&;锁);
的printf(德尔塔数:%d \\ n,CP-GT&; cars_in - CP-GT&; cars_out);
/ *释放锁定* /
调用pthread_mutex_unlock(安培; CP-GT&;锁);
} 收益率((无效*)NULL);
}/ *初始化* /
静态INT
的init(cp_t * CP,INT容量)
{ / *设置界缓冲区内部* /
CP-GT&;占用= CP-GT&; nextin = CP-GT&; nextout = CP-GT&; cars_in = CP-GT&; cars_out = 0;
CP-GT&;容量=能力;
/ *初始化我们的数据结构* /
CP-GT&;停车场=(INT *)malloc的(CP-GT&;能力的sizeof *(* CP-GT&;停车场));
/ *检查malloc的成功* / 如果(CP-GT&;停车场== NULL){
PERROR(malloc()函数);
出口(EXIT_FAILURE);
} / *初始化锁和条件变量* /
调用pthread_mutex_init(安培; CP-GT&;锁,NULL);
pthread_cond_init(&放大器; CP->空间,NULL);
pthread_cond_init(安培; CP-GT&;汽车,NULL);
/ *种子的随机数发生器* /
函数srand((unsigned int类型)GETPID()); 返回(0);
}INT
主(INT ARGC,CHAR *的argv [])
{ 的pthread_t P,C,M;
cp_t CP;
/ *检查使用* / 如果(的argc!= 2){
的printf(用法:%s的BUFFER_SIZE \\ n,argv的[0]);
出口(EXIT_FAILURE);
} / *初始化* /
的init(安培; CP,与atoi(ARGV [1]));
/ *创建线程我们* /
在pthread_create(安培; P,NULL,制片人,(无效*)及厘泊);
在pthread_create(安培; P,NULL,制片人,(无效*)及厘泊);
在pthread_create(和C,NULL,消费(无效*)及CP);
在pthread_create(和C,NULL,消费(无效*)及CP);
在pthread_create(安培峰; m,NULL,监视器,(无效*)及厘泊);
/ *等待我们的线程* /
在pthread_join(对,NULL);
在pthread_join(对,NULL);
在pthread_join(C,NULL);
在pthread_join(C,NULL);
在pthread_join(男,NULL); 返回(0);
}
我或许可以给你完整的答案,但我担心的Lasse五卡尔森的。所以,我给你的提示。
- 屏障对象
栏
是在结构cp_t
已接近 - 与
pthread_barrier_init
初始化它,就像你初始化互斥。有计数
和演员的数量之间的对应关系。 - 无论是生产者和消费者需要的
等
开始之前生产/消费。明白了吗?
Hi Sorry for posting a big dump of code but I'm very new at C code, Basically I'm doing an assignment for college and and I have to implement a "pthread_barrier", now I understand the concept of the barrier (or at least I think I do) But I am just not sure exactly where I should put it. The assignment states:
"Use pthread_barrier_init and pthread_barrier_wait to ensure that all producer/consumer threads begin producing/consuming at the same time."
This is the extra credit part of the assignment by the way
#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 2
typedef 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 int
init(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);
}
int
main(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, NULL, producer, (void *)&cp);
pthread_create(&c, NULL, consumer, (void *)&cp);
pthread_create(&c, NULL, consumer, (void *)&cp);
pthread_create(&m, NULL, monitor, (void *)&cp);
/* Wait for our threads */
pthread_join(p, NULL);
pthread_join(p, NULL);
pthread_join(c, NULL);
pthread_join(c, NULL);
pthread_join(m, NULL);
return (0);
}
I could probably give you the full answer but I am afraid of Lasse V. Karlsen. So I'll give you hints.
- The barrier object
bar
is already accessible in yourstruct cp_t
- Initialize it with
pthread_barrier_init
, like you initialized the mutexes. There is a correspondence betweencount
and the number of actors. - Both the producers and consumers need to
wait
before starting to produce / consume. Get it ?
这篇关于如何使用pthreads的屏障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!