trylock:
#include <stdio.h> #include <unistd.h> #include <pthread.h> int beginnum=1000; pthread_rwlock_t rwlock=PTHREAD_RWLOCK_INITIALIZER; void* thr_write(void* arg){ while(1){ pthread_rwlock_wrlock(&rwlock); printf("--%s--%lu--beginnum--%d ",__FUNCTION__,pthread_self(),++beginnum); usleep(2000); pthread_rwlock_unlock(&rwlock); usleep(6000); } return NULL; } void* thr_read(void* arg){ while(1){ pthread_rwlock_rdlock(&rwlock); printf("--%s--%lu--beginnum--%d ",__FUNCTION__,pthread_self(),beginnum); usleep(2000); pthread_rwlock_unlock(&rwlock); usleep(2000); } return NULL; } int main() { int n=8, i=0; pthread_t tid[8]; for(i=0;i<5;i++){ pthread_create(&tid[i],NULL,thr_read,NULL); } for(;i<8;i++){ pthread_create(&tid[i],NULL,thr_write,NULL); } for(i=0;i<8;i++){ pthread_join(tid[i],NULL); } return 0; }
条件变量
生产者消费者模型:
#include <stdio.h> #include <unistd.h> #include <pthread.h> #include <string.h> #include <stdlib.h> pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond=PTHREAD_COND_INITIALIZER; int beginnum=100; typedef struct _ProdInfo{ int num; struct _ProdInfo* next; }ProdInfo; ProdInfo* head=NULL; void* thr_producer(void* arg){ while(1){ ProdInfo* prod=malloc(sizeof(ProdInfo)); prod->num=beginnum++; printf("----%s----self=%lu---%d ", __FUNCTION__,pthread_self(),prod->num); pthread_mutex_lock(&mutex); prod->next=head; head=prod; pthread_mutex_unlock(&mutex); pthread_cond_signal(&cond); sleep(rand()%2); } return NULL; } void* thr_customer(void* arg){ ProdInfo *prod=NULL; while(1){ pthread_mutex_lock(&mutex); while(head==NULL){ pthread_cond_wait(&cond,&mutex); } prod=head; head=head->next; printf("----%s----self=%lu---%d ", __FUNCTION__,pthread_self(),prod->num); pthread_mutex_unlock(&mutex); sleep(rand()%4); } return NULL; } int main() { pthread_t tid[3]; pthread_create(&tid[0],NULL,thr_producer,NULL); pthread_create(&tid[1],NULL,thr_customer,NULL); pthread_create(&tid[2],NULL,thr_customer,NULL); pthread_join(tid[0],NULL); pthread_join(tid[1],NULL); pthread_join(tid[2],NULL); pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond); return 0; }
利用信号量实现生产者消费者:
#include <stdio.h> #include <unistd.h> #include <pthread.h> #include <semaphore.h> #include <stdlib.h> sem_t blank,xfull; //blank空位数,xfull饼数 int queue[5]; int beginnum=100; void* thr_producer(void* arg){ int i=0; while(1){ sem_wait(&blank); printf("----%s----self:%lu----num:%d---- ",__FUNCTION__,pthread_self(),beginnum); queue[(i++)%5]=beginnum++; sem_post(&xfull); sleep(rand()%3); } } void* thr_customer(void* arg){ int i=0; int num=0; while(1){ sem_wait(&xfull); num=i%5; i++; printf("----%s----self:%lu----num:%d---- ",__FUNCTION__,pthread_self(),queue[num]); sem_post(&blank); sleep(rand()%3); } } int main() { sem_init(&blank,0,5); //最开始空闲5个空位 sem_init(&xfull,0,0); //最开始没有饼 pthread_t tid[2]; pthread_create(&tid[0],NULL,thr_producer,NULL); pthread_create(&tid[0],NULL,thr_customer,NULL); pthread_join(tid[0],NULL); pthread_join(tid[1],NULL); sem_destroy(&blank); sem_destroy(&xfull); return 0; }