当前位置: 技术问答>linux和unix
求助多线程并发的问题——rechard stevens网络编程第二卷例题
来源: 互联网 发布时间:2016-08-31
本文导语: #include "../unpipc.h" #include "../apuerror.h" #include "pthread.h" #define MAXNTHREADS 5 #define MAXNITEMS 10000 int nitems; struct { pthread_mutex_t mutex; int buff[MAXNITEMS]; int nput; int nval; } share...
#include "../unpipc.h"
#include "../apuerror.h"
#include "pthread.h"
#define MAXNTHREADS 5
#define MAXNITEMS 10000
int nitems;
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:prodcons2");
//nitems = min(atoi(argv[1]),MAXNITEMS);
//nthreads = min(atoi(argv[2]),MAXNTHREADS);
nitems = MAXNITEMS;
nthreads = MAXNTHREADS;
if(pthread_setconcurrency(nthreads)!=0)
printf("setconvrrency error ");
int j=pthread_getconcurrency();
printf("number of concurrency is %dn",j);
for(i=0;i
#include "../apuerror.h"
#include "pthread.h"
#define MAXNTHREADS 5
#define MAXNITEMS 10000
int nitems;
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:prodcons2");
//nitems = min(atoi(argv[1]),MAXNITEMS);
//nthreads = min(atoi(argv[2]),MAXNTHREADS);
nitems = MAXNITEMS;
nthreads = MAXNTHREADS;
if(pthread_setconcurrency(nthreads)!=0)
printf("setconvrrency error ");
int j=pthread_getconcurrency();
printf("number of concurrency is %dn",j);
for(i=0;i