# include <limits.h> # include <pthread.h> # include <stdlib.h> # include <stdio.h> # define MAXNTHREADS 100 # define NBUFF 20 typedef struct { pthread_cond_t cond; int resource; } Cond; typedef struct { pthread_t id; int seq; } Data; Cond getCond = { PTHREAD_COND_INITIALIZER, 0 }; Cond putCond = { PTHREAD_COND_INITIALIZER, NBUFF }; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; Data buff[NBUFF]; int front = 0; int rear = 0; int size = 0; int run = 1; void stop() { run = 0; pthread_mutex_lock(&mutex); getCond.resource = INT_MAX / 2; pthread_cond_broadcast(&getCond.cond); putCond.resource = INT_MAX / 2; pthread_cond_broadcast(&putCond.cond); pthread_mutex_unlock(&mutex); } int put(const Data* data) { pthread_mutex_lock(&mutex); while( putCond.resource < 1 ) pthread_cond_wait(&putCond.cond, &mutex); putCond.resource--; if ( run ) { buff[rear] = *data; rear = (rear + 1) / NBUFF; size++; } getCond.resource++; pthread_cond_signal(&getCond.cond); pthread_mutex_unlock(&mutex); return run; } int get(Data* data) { int rc; pthread_mutex_lock(&mutex); while( getCond.resource < 1 ) pthread_cond_wait(&getCond.cond, &mutex); getCond.resource--; if ( size != 0 ) {/* not empty */ *data = buff[front]; front = (front + 1) % NBUFF; size--; rc = 1; } else rc = 0; putCond.resource++; pthread_cond_signal(&putCond.cond); pthread_mutex_unlock(&mutex); return rc; } int nitems, nputs, ngets; void * put_thread(void *arg) { Data data; data.id = pthread_self(); for ( data.seq = 0; data.seq < nitems; data.seq++ ) { put(&data); *((int *)arg) += 1; } return 0; } void * get_thread(void *arg) { pthread_t tid; Data data; tid = pthread_self(); while( get(&data) ) { printf("%lu %lu %d\n", tid, data.id, data.seq); *((int *)arg) += 1; } return 0; } int main(int argc, char **argv) { int i, p, g; int putcount[MAXNTHREADS], getcount[MAXNTHREADS]; pthread_t tid_put[MAXNTHREADS], tid_get[MAXNTHREADS]; if ( argc < 4 ) { fprintf(stderr, "Usage: %s <#items> <#put> <#get>\n", argv[0]); return -1; } nitems = atoi(argv[1]); nputs = atoi(argv[2]); if ( nputs > MAXNTHREADS ) nputs = MAXNTHREADS; ngets = atoi(argv[3]); if ( ngets > MAXNTHREADS ) ngets = MAXNTHREADS; printf("Item: <%d> Puts: <%d> Gets: <%d>\n", nitems, nputs, ngets); for ( i = 0; i < nputs; i++ ) { putcount[i] = 0; pthread_create(&tid_put[i], 0, put_thread, &putcount[i]); } for ( i = 0; i < ngets; i++ ) { getcount[i] = 0; pthread_create(&tid_get[i], 0, get_thread, &getcount[i]); } p = 0; for ( i = 0; i < nputs; i++ ) { pthread_join(tid_put[i], 0); printf("Put %lu count[%d] = %d\n", tid_put[i], i, putcount[i]); p += putcount[i]; } stop(); g = 0; for ( i = 0; i < ngets; i++ ) { pthread_join(tid_get[i], 0); g += getcount[i]; printf("Get %lu count[%d] = %d\n", tid_get[i], i, getcount[i]); } printf("Get %d Put %d\n", g, p); return 0; }
本文来自CSDN博客,转载请标明出处:http:
|