多线程学习系列三 多线程实现读者和写者问题
2015-08-21 13:24
399 查看
设置读线程是两个,写线程一个,用读写锁实现。
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#define RNUM 2
pthread_rwlock_t lock;
struct Data
{
int a;
float b;
};
struct Data *pdata = NULL;
void *Read(void *num)
{
int i = 0;
while( i++ != 3)
{
pthread_rwlock_rdlock(&lock);
if(pdata == NULL)
printf("reader[%d]:%u is reading data ! data is NULL\n",(int)num,(int)pthread_self());
else
printf("reader[%d]:%u is reading data! data: %d %f \n",(int)num,(int)pthread_self(),pdata->a,pdata->b);
pthread_rwlock_unlock(&lock);
}
return (void *)0;
}
void *Write(void *num)
{
int i = 0;
while(i++ != 3)
{
pthread_rwlock_wrlock(&lock);
if(pdata == NULL)
{
pdata = (struct Data *)malloc(sizeof(struct Data));
pdata->a = 23 + i;
pdata->b = 12.3 + i;
printf("writer:%u is writing data ! %d %f\n",(int)pthread_self(),pdata->a,pdata->b);
}
else
{
free(pdata);
pdata = NULL;
printf("writer:%u writer free the data!\n",(int)pthread_self());
}
pthread_rwlock_unlock(&lock);
}
return (void *)0;
}
int main()
{
pthread_t reader[RNUM];
pthread_t writer;
for(int i=0;i<RNUM;i++)
pthread_create(&reader[i],NULL,Read,(void *)i);
pthread_create(&writer,NULL,Write,NULL);
pthread_join(writer,NULL);
for(int i=0;i<RNUM;i++)
pthread_join(reader[i],NULL);
printf("Main thread:%u is finished!\n",(int)pthread_self());
return 0;
}
运行结果:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#define RNUM 2
pthread_rwlock_t lock;
struct Data
{
int a;
float b;
};
struct Data *pdata = NULL;
void *Read(void *num)
{
int i = 0;
while( i++ != 3)
{
pthread_rwlock_rdlock(&lock);
if(pdata == NULL)
printf("reader[%d]:%u is reading data ! data is NULL\n",(int)num,(int)pthread_self());
else
printf("reader[%d]:%u is reading data! data: %d %f \n",(int)num,(int)pthread_self(),pdata->a,pdata->b);
pthread_rwlock_unlock(&lock);
}
return (void *)0;
}
void *Write(void *num)
{
int i = 0;
while(i++ != 3)
{
pthread_rwlock_wrlock(&lock);
if(pdata == NULL)
{
pdata = (struct Data *)malloc(sizeof(struct Data));
pdata->a = 23 + i;
pdata->b = 12.3 + i;
printf("writer:%u is writing data ! %d %f\n",(int)pthread_self(),pdata->a,pdata->b);
}
else
{
free(pdata);
pdata = NULL;
printf("writer:%u writer free the data!\n",(int)pthread_self());
}
pthread_rwlock_unlock(&lock);
}
return (void *)0;
}
int main()
{
pthread_t reader[RNUM];
pthread_t writer;
for(int i=0;i<RNUM;i++)
pthread_create(&reader[i],NULL,Read,(void *)i);
pthread_create(&writer,NULL,Write,NULL);
pthread_join(writer,NULL);
for(int i=0;i<RNUM;i++)
pthread_join(reader[i],NULL);
printf("Main thread:%u is finished!\n",(int)pthread_self());
return 0;
}
运行结果:
相关文章推荐
- 辛星简译MySQL中的last_query_cost
- 负载均衡下获取客户端ip地址
- mysql 修改root密码 admin数据库消失解决方法
- SkylineGlobe 如何二次开发实现天际线分析功能
- mysql 命令大全
- myeclipse新建webservice
- Android设置状态栏颜色
- tableViewCell上使用collectionView加载自定义collectionViewCell
- 观察者模式
- 2.1 堆数据表
- iOS性能优化:Instruments使用实战
- Oracle 树操作(select…start with…connect by…prior)
- Xcode7 真机调试
- mysql主从备份读写分享
- Java多线程实践—篇外篇
- hdu3339(Dijkstra+背包)
- 为何ViewController释放后不走dealloc
- Python数据结构:映射(map)——字典
- 626 intersection set
- Yii: 扩展CGridView增加导出CSV功能