#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
//读写锁
int number = 10000;
//create rwlock
pthread_rwlock_t lock;
void* write_func(void *arg)
{
while(1)
{
pthread_rwlock_wrlock(&lock);
number++;
printf("write:%ld,%d\n",pthread_self(),number);
pthread_rwlock_unlock(&lock);
usleep(500);
}
}
void* read_func(void *arg)
{
while(1)
{
pthread_rwlock_rdlock(&lock);
printf("read:%ld,%d\n",pthread_self(),number);
pthread_rwlock_unlock(&lock);
usleep(500);
}
}
int main()
{
//init lock
pthread_rwlock_init(&lock,NULL);
pthread_t p[8];
for(int i=0;i<3;i++)
{
pthread_create(&p[i],NULL,write_func,NULL);
}
for(int i=3;i<8;i++)
{
pthread_create(&p[i],NULL,read_func,NULL);
}
for(int i=0;i<8;i++)
{
pthread_join(p[i],NULL);
}
//destroy lock
pthread_rwlock_destroy(&lock);
return 0;
}