当前位置: 技术问答>linux和unix
mutex问题,UNP2上的程序
来源: 互联网 发布时间:2016-04-21
本文导语: /* include main */ #include #include #include "math.h" #include #define MAXNITEMS 1000000 #define MAXNTHREADS 100 int nitems; /* read-only by producer and consumer */ struct { pthread_mutex_t mutex; int b...
/* include main */
#include
#include
#include "math.h"
#include
#define MAXNITEMS 1000000
#define MAXNTHREADS 100
int nitems; /* read-only by producer and consumer */
struct {
pthread_mutex_t mutex;
int buff[MAXNITEMS];
int nput;
int nval;
} shared = { PTHREAD_MUTEX_INITIALIZER };
void *produce(void *), *consume(void *);
int
main(int argc, char **argv)
{
int i, nthreads, count[MAXNTHREADS];
pthread_t tid_produce[MAXNTHREADS], tid_consume;
if (argc != 3)
err_quit("usage: prodcons1 ");
nitems = atoi(argv[1]);
nthreads = atoi(argv[2]);
pthread_setconcurrency(nthreads);
for (i = 0; i