Linux多线程──读者写者问题
2012-07-12 21:37
197 查看
读者写者问题
这也是一个非常经典的多线程题目,题目大意如下:有一个写者很多读者,多个读者可以同时读文件,但写者在写文件时不允许有读者在读文件,同样有读者读时写者也不能写。
程序:
// reader_writer.cpp
//////////////////////////////////////////////////////////////////////
// 读者写者问题
// 有一个写者很多读者,多个读者可以同时读文件,但写者在写文件时不允许有读者在读文件,
// 同样有读者读时写者也不能写。
//////////////////////////////////////////////////////////////////////
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
// 定义数据类
class data
{
public:
data(int i, float f):
I(i), F(f)
{}
int I;
float F;
};
// 读者写者读写的内容
data *p_data = NULL;
pthread_rwlock_t lock;
// 写者数目
const int WRITER_NUMBER = 2;
void *reader(void *arg);
void *writer(void *arg);
int main(int argc, char **argv)
{
pthread_t reader_tid;
pthread_t writer_tid[WRITER_NUMBER];
pthread_create(&reader_tid, NULL, reader, NULL);
for (int i = 0; i < WRITER_NUMBER; ++i)
{
pthread_create(&writer_tid[i], NULL, writer, (void *)i);
}
sleep(1);
return 0;
}
void *reader(void *arg)
{
int id = (int)arg;
pthread_detach(pthread_self());
while (true)
{
pthread_rwlock_rdlock(&lock);
printf("reader %d is reading the data; ", id);
if (p_data == NULL)
{
printf("the data is NULL\n");
}
else
{
printf("the data is (%d, %f)\n", p_data->I, p_data->F);
}
pthread_rwlock_unlock(&lock);
}
return (void *)0;
}
void *writer(void *arg)
{
pthread_detach(pthread_self());
while (true)
{
pthread_rwlock_wrlock(&lock);
printf("writer is writing the data; ");
if (p_data == NULL)
{
p_data = new data(1, 1.1f);
printf("writer create the data (%d, %f)\n", p_data->I, p_data->F);
}
else
{
delete p_data;
p_data = NULL;
printf("writer free the data\n");
}
pthread_rwlock_unlock(&lock);
}
return (void *)0;
}
这也是一个非常经典的多线程题目,题目大意如下:有一个写者很多读者,多个读者可以同时读文件,但写者在写文件时不允许有读者在读文件,同样有读者读时写者也不能写。
程序:
// reader_writer.cpp
//////////////////////////////////////////////////////////////////////
// 读者写者问题
// 有一个写者很多读者,多个读者可以同时读文件,但写者在写文件时不允许有读者在读文件,
// 同样有读者读时写者也不能写。
//////////////////////////////////////////////////////////////////////
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
// 定义数据类
class data
{
public:
data(int i, float f):
I(i), F(f)
{}
int I;
float F;
};
// 读者写者读写的内容
data *p_data = NULL;
pthread_rwlock_t lock;
// 写者数目
const int WRITER_NUMBER = 2;
void *reader(void *arg);
void *writer(void *arg);
int main(int argc, char **argv)
{
pthread_t reader_tid;
pthread_t writer_tid[WRITER_NUMBER];
pthread_create(&reader_tid, NULL, reader, NULL);
for (int i = 0; i < WRITER_NUMBER; ++i)
{
pthread_create(&writer_tid[i], NULL, writer, (void *)i);
}
sleep(1);
return 0;
}
void *reader(void *arg)
{
int id = (int)arg;
pthread_detach(pthread_self());
while (true)
{
pthread_rwlock_rdlock(&lock);
printf("reader %d is reading the data; ", id);
if (p_data == NULL)
{
printf("the data is NULL\n");
}
else
{
printf("the data is (%d, %f)\n", p_data->I, p_data->F);
}
pthread_rwlock_unlock(&lock);
}
return (void *)0;
}
void *writer(void *arg)
{
pthread_detach(pthread_self());
while (true)
{
pthread_rwlock_wrlock(&lock);
printf("writer is writing the data; ");
if (p_data == NULL)
{
p_data = new data(1, 1.1f);
printf("writer create the data (%d, %f)\n", p_data->I, p_data->F);
}
else
{
delete p_data;
p_data = NULL;
printf("writer free the data\n");
}
pthread_rwlock_unlock(&lock);
}
return (void *)0;
}
相关文章推荐
- Linux多线程──读者写者问题
- linux 读者写者问题 读者优先c++
- William Stallings 《操作系统内核和设计原理》书中Linux下C语言实现读者写者问题(写者优先)代码
- 【Linux学习笔记】39:Linux下C模拟读者写者问题
- linux多线程编程——读者优先、写者优先问题
- 经典同步问题linux下的C实现:生产者-消费者问题,读者-写者问题,哲学家问题
- Linux 关于读者与写者同步互斥问题的解析
- 经典同步问题linux下的C实现:生产者-消费者问题,读者-写者问题,哲学家问题
- Linux多线程实践(6) --Posix读写锁解决读者写者问题
- Linux多线程实践(6) --Posix读写锁解决读者写者问题
- Linux多线程实践(6) --Posix读写锁解决读者写者问题
- linux中关于IPC(一部分涉及读者写者问题)
- 多线程同步中的读者写者问题 - linux 平台实现
- Linux多线程实践(6) --Posix读写锁解决读者写者问题
- 读者写者问题
- 读者写者问题(读者优先、写者优先、公平竞争)
- 读者---写者问题
- 读者——写者问题
- 读者与写者问题
- Java 多线程读者写者问题