1.互斥量
- #include <stdlib.h>
- #include <pthread.h>
- struct foo {
- int f_count;
- pthread_mutex_t f_lock;
- };
- struct foo *
- foo_alloc(void) /* allocate the object */
- {
- struct foo *fp;
- if ((fp = malloc(sizeof(struct foo)))!=NULL) {
- fp->count = 1;
- if (pthread_mutex_init(&fp->lock, NULL) != 0) {
- free(fp);
- return NULL;
- }
- /* continue initialization */
- }
- return fp;
- }
- void
- foo_add(struct foo *fp) { /* add an reference to this object */
- pthread_mutex_lock(&fp->f_lock);
- fp->count++;
- pthread_mutex_unlock(&fp->f_lock);
- }
- void
- foo_rele(struct foo *fp) { /* release a reference to the object */
- pthread_mutex_lock(&fp->f_lock);
- if (--fp->f_count == 0) /* last reference */
- pthread_mutex_unlock(&fp->f_lock);
- pthread_mutex_destroy(&fp->f_lock);
- free(fp);
- } else {
- pthread_mutex_unlock(&fp->f_lock);
- }
- }
- /*作业请求作业*/
- #include <stdlib.h>
- #include <pthread.h>
- struct job {
- struct job *j_next;
- struct job *j_prev;
- pthread_t j_id;
- /* something more */
- }
- struct queue {
- struct job *q_head;
- struct job *q_tail;
- pthread_rwlock_t q_lock;
- };
- /*
- * Initialize a queue
- */
- int
- queue_init(struct queue *qp)
- {
- int err;
- qp->q_head = NULL;
- qp->q_tail = NULL;
- err = pthread_rwlock_init(&qp->q_lock, NULL);
- if (err != 0)
- return(err);
- return 0;
- }
- /*
- * Insert a job at the head of the queue
- */
- void
- job_insert(struct queue *qp, struct job *jp)
- {
- pthread_rwlock_wrlock(&qp->q_lock);
- jp->j_next = qp->q_head;
- jp->j_prev = NULL;
- if (qp->q_head != NULL)
- qp->q_head->j_prev = jp;
- else
- qp->q_tail->j_prev = jp;
- qp->q_head = jp;
- pthread_rwlock_unlock(&qp->q_lock);
- }
- /*
- * Append a job on the tail of the queue
- */
- void
- job_append(struct queue *qp, struct job *jp)
- {
- pthread_rwlock_wrlock(&qp->q_lock);
- jp->j_next = NULL;
- jp->j_prev = qp->q_tail;
- if (qp->q_tail != NULL)
- qp->q_tail->q_next = jp;
- else
- qp->q_head = jp;
- qp->q_tail = jp;
- }
- /*
- * Remove the given job from a queue
- */
- void
- job_remove(struct queue *qp, struct job *jp)
- {
- pthread_rwlock_wrlock(qp->q_lock);
- if (qp->head == jp) {
- qp->head = jp->q_next;
- if (qp->tail == jp)
- qp->tail = NULL;
- }
- else if (qp->tail == jp)
- qp->tail = jp->q_prev;
- else {
- jp->q_prev->q->next = jp->q_next;
- jp->q_next->q->prev = jp->q_prev;
- }
- free(jp);
- pthread_rwlock_unlock(qp->lock);
- }
- /*
- * Find a job for the given thread ID
- */
- struct job *
- job_find(struct queue *qp, pthread_t id)
- {
- struct job *jp;
- if (pthread_rwlock_rdlock(&qp->lock) != 0)
- return(NULL);
- for (jp = qp->q_head; jp != NULL; jp = jp->j_next)
- if (pthread_equal(jp->j_id, id))
- break;
- pthread_rwlock_unlock(&qp->lock)
- return jp;
- }
- pthread_mutex_t count_lock;
- pthread_cond_t count_nonzero;
- unsigned count;
- decrement_count () {
- pthread_mutex_lock (&count_lock);
- while(count == 0)
- pthread_cond_wait( &count_nonzero, &count_lock);
- count=count -1;
- pthread_mutex_unlock (&count_lock);
- }
- increment_count(){
- pthread_mutex_lock(&count_lock);
- if(count == 0)
- pthread_cond_signal(&count_nonzero);
- count=count+1;
- pthread_mutex_unlock(&count_lock);
- }
- /* File sem.c */
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- #define MAXSTACK 100
- int stack[MAXSTACK][2];
- int size=0;
- sem_t sem;
- /*
- * 从文件1.dat读取数据,每读一次,信号量加1
- */
- void ReadData1(void) {
- FILE *fp=fopen("1.dat","r");
- while(!feof(fp)){
- fscanf(fp,"%d %d",&stack[size][0],&stack[size][1]);
- sem_post(&sem);
- ++size;
- }
- fclose(fp);
- }
- /*
- * 从文件2.dat读取数据
- */
- void ReadData2(void){
- FILE *fp=fopen("2.dat","r");
- while(!feof(fp)){
- fscanf(fp,"%d %d",&stack[size][0],&stack[size][1]);
- sem_post(&sem);
- ++size;
- }
- fclose(fp);
- }
- /*
- *阻塞等待缓冲区有数据,读取数据后,释放空间,继续等待
- */
- void HandleData1(void) {
- while(1){
- sem_wait(&sem);
- printf("Plus:%d+%d=%dn",stack[size][0],stack[size][1],
- stack[size][0]+stack[size][1]);
- --size;
- }
- }
- void
- HandleData2(void) {
- while(1) {
- sem_wait(&sem);
- printf("Multiply:%d*%d=%dn",stack[size][0],stack[size][1],
- stack[size][0]*stack[size][1]);
- --size;
- }
- }
- int
- main(void) {
- pthread_t t1,t2,t3,t4;
- sem_init(&sem,0,0);
- pthread_create(&t1,NULL,(void *)HandleData1,NULL);
- pthread_create(&t2,NULL,(void *)HandleData2,NULL);
- pthread_create(&t3,NULL,(void *)ReadData1,NULL);
- pthread_create(&t4,NULL,(void *)ReadData2,NULL);
- /* 防止程序过早退出,让它在此无限期等待*/
- pthread_join(t1,NULL);
- }