int main()
{
printf("多线程,读者优先\n");
pthread_mutex_init(&mutex_write,NULL);
sem_init(&sem_read,0,1);
int i = 0;
for(i = 0; i < N_WRITER; i++)
{
pthread_create(&wid[i],NULL,writer,NULL);
}
for(i = 0; i < N_READER; i++)
{
pthread_create(&rid[i],NULL,reader,NULL);
}
sleep(1);
return 0;
}
读者优先
为了更明显的看到效果,在main函数中创建了20个写者和5个读者。注意编译时要加上-lpthread指定库。
写者优先写者优先与读者优先的不同是:如果读者来,有写者等待,但有其他读者正在读,新读者等。
使用两个互斥锁mutex_write,mutex_read和两个信号量sem_read,sem_write来确保访问资源的互斥和同步。
#include "stdio.h"
#include <stdlib.h>
#include <pthread.h>
#include<semaphore.h>
#define N_WRITER 5 //写者数目
#define N_READER 20 //读者数目
#define W_SLEEP 1 //控制写频率
#define R_SLEEP 0.5 //控制读频率
pthread_t wid[N_WRITER],rid[N_READER];
int data = 0;
int readerCnt = 0, writerCnt = 0;
pthread_mutex_t sem_read;
pthread_mutex_t sem_write;
pthread_mutex_t mutex_write;
pthread_mutex_t mutex_read;
void write()
{
int rd = rand();
printf("write %d\n",rd);
data = rd;
}
void read()
{
printf("read %d\n",data);
}
void * writer(void * in)
{
// while(1)
// {
sem_wait(&sem_write);
{//临界区,希望修改 writerCnt,独占 writerCnt
writerCnt++;
if(writerCnt == 1){
//阻止后续的读者加入待读队列
pthread_mutex_lock(&mutex_read);
}
}
sem_post(&sem_write);
pthread_mutex_lock(&mutex_write);
{//临界区,限制只有一个写者修改数据
printf("写线程id%d进入数据集\n",pthread_self());
write();
printf("写线程id%d退出数据集\n",pthread_self());
}
pthread_mutex_unlock(&mutex_write);
sem_wait(&sem_write);
{//临界区,希望修改 writerCnt,独占 writerCnt
writerCnt--;
if(writerCnt == 0){
//阻止后续的读者加入待读队列
pthread_mutex_unlock(&mutex_read);
}
}
sem_post(&sem_write);
sleep(W_SLEEP);
// }
pthread_exit((void *) 0);
}
void * reader (void * in)
{
// while(1)
// {
//假如写者锁定了mutex_read,那么成千上万的读者被锁在这里
pthread_mutex_lock(&mutex_read);//只被一个读者占有
{//临界区
sem_wait(&sem_read);//代码段 1
{//临界区
readerCnt++;
if(readerCnt == 1){
pthread_mutex_lock(&mutex_write);
}
}
sem_post(&sem_read);
}
pthread_mutex_unlock(&mutex_read);//释放时,写者将优先获得mutex_read
printf("读线程id%d进入数据集\n",pthread_self());
read();
printf("读线程id%d退出数据集\n",pthread_self());
sem_wait(&sem_read);//代码段2
{//临界区
readerCnt--;
if(readerCnt == 0){
pthread_mutex_unlock(&mutex_write);//在最后一个并发读者读完这里开始禁止写者执行写操作
}
}
sem_post(&sem_read);
sleep(R_SLEEP);
// }
pthread_exit((void *) 0);
}