linux编程---线程---读写锁
来源:互联网 发布:大数据编程 书籍推荐 编辑:程序博客网 时间:2024/06/07 00:43
读写锁通信机制
读写锁分为读锁和写锁,功能如下
(1)如果某线程申请了读锁,其他线程可以再申请读锁,但不能申请写锁。
(2)如果某线程申请了写锁,则其他线程不能申请读锁,也不能申请写锁。
初始化读写锁
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
const pthread_rwlockattr_t *restrict attr);
销毁读写锁
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
申请读锁
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
申请写锁
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
解锁
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
程序示例:
程序实现4个线程,两个读线程,两个写线程。
#include<unistd.h>
#include<stdio.h>
#include<errno.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
static pthread_rwlock_t rwlock;
#define BUFSIZE 1024
char gdata[BUFSIZE];
int gtime;
void *thread_fun_read1(void*arg);
void *thread_fun_read2(void*arg);
void *thread_fun_write1(void*arg);
void *thread_fun_write2(void*arg);
int main(int argc,char* argv[])
{
int res;
pthread_t th1,th2,th3,th4;
void* thret;
res = pthread_rwlock_init(&rwlock,NULL);
if(res != 0)
{
perror("rwlock initialization failed");
exit(-1);
}
res = pthread_create(&th1,NULL,thread_fun_read1,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_create(&th2,NULL,thread_fun_read2,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_create(&th3,NULL,thread_fun_write1,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_create(&th4,NULL,thread_fun_write2,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_join(th1,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
res = pthread_join(th2,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
res = pthread_join(th3,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
res = pthread_join(th4,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
pthread_rwlock_destroy(&rwlock);
exit(0);
}
void *thread_fun_read1(void*arg)
{
printf("thread read one try to get lock\n");
pthread_rwlock_rdlock(&rwlock);
while(strncmp("end",gdata,3) != 0)
{
printf("this is thread read one. %s \n",gdata);
pthread_rwlock_unlock(&rwlock);
sleep(2);
pthread_rwlock_rdlock(&rwlock);
while(gdata[0] == '\0')
{
pthread_rwlock_unlock(&rwlock);
sleep(2);
pthread_rwlock_rdlock(&rwlock);
}
}
pthread_rwlock_unlock(&rwlock);
gtime = 1;
pthread_exit(0);
}
void *thread_fun_read2(void*arg)
{
printf("thread read two try to get lock\n");
pthread_rwlock_rdlock(&rwlock);
while(strncmp("end",gdata,3) != 0)
{
printf("this is thread read two. %s \n",gdata);
pthread_rwlock_unlock(&rwlock);
sleep(5);
pthread_rwlock_rdlock(&rwlock);
while(gdata[0] == '\0')
{
pthread_rwlock_unlock(&rwlock);
sleep(5);
pthread_rwlock_rdlock(&rwlock);
}
}
pthread_rwlock_unlock(&rwlock);
gtime = 1;
pthread_exit(0);
}
void *thread_fun_write1(void*arg)
{
printf("this is write thread one try to get lock\n");
while(!gtime)
{
pthread_rwlock_wrlock(&rwlock);
printf("this is write thread one.\n input some text,enter 'end'to finish\n");
fgets(gdata,BUFSIZE,stdin);
pthread_rwlock_unlock(&rwlock);
sleep(15);
}
pthread_rwlock_unlock(&rwlock);
pthread_exit(0);
}
void *thread_fun_write2(void*arg)
{
printf("this is write thread two try to get lock\n");
while(!gtime)
{
pthread_rwlock_wrlock(&rwlock);
printf("this is write thread two.\n input some text,enter 'end'to finish\n");
fgets(gdata,BUFSIZE,stdin);
pthread_rwlock_unlock(&rwlock);
sleep(15);
}
pthread_rwlock_unlock(&rwlock);
pthread_exit(0);
}
读写锁分为读锁和写锁,功能如下
(1)如果某线程申请了读锁,其他线程可以再申请读锁,但不能申请写锁。
(2)如果某线程申请了写锁,则其他线程不能申请读锁,也不能申请写锁。
初始化读写锁
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
const pthread_rwlockattr_t *restrict attr);
销毁读写锁
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
申请读锁
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
申请写锁
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
解锁
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
程序示例:
程序实现4个线程,两个读线程,两个写线程。
#include<unistd.h>
#include<stdio.h>
#include<errno.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
static pthread_rwlock_t rwlock;
#define BUFSIZE 1024
char gdata[BUFSIZE];
int gtime;
void *thread_fun_read1(void*arg);
void *thread_fun_read2(void*arg);
void *thread_fun_write1(void*arg);
void *thread_fun_write2(void*arg);
int main(int argc,char* argv[])
{
int res;
pthread_t th1,th2,th3,th4;
void* thret;
res = pthread_rwlock_init(&rwlock,NULL);
if(res != 0)
{
perror("rwlock initialization failed");
exit(-1);
}
res = pthread_create(&th1,NULL,thread_fun_read1,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_create(&th2,NULL,thread_fun_read2,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_create(&th3,NULL,thread_fun_write1,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_create(&th4,NULL,thread_fun_write2,NULL);
if(res != 0)
{
perror("thread create failed");
exit(-1);
}
res = pthread_join(th1,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
res = pthread_join(th2,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
res = pthread_join(th3,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
res = pthread_join(th4,&thret);
if(res != 0)
{
perror("thread join failed");
exit(-1);
}
pthread_rwlock_destroy(&rwlock);
exit(0);
}
void *thread_fun_read1(void*arg)
{
printf("thread read one try to get lock\n");
pthread_rwlock_rdlock(&rwlock);
while(strncmp("end",gdata,3) != 0)
{
printf("this is thread read one. %s \n",gdata);
pthread_rwlock_unlock(&rwlock);
sleep(2);
pthread_rwlock_rdlock(&rwlock);
while(gdata[0] == '\0')
{
pthread_rwlock_unlock(&rwlock);
sleep(2);
pthread_rwlock_rdlock(&rwlock);
}
}
pthread_rwlock_unlock(&rwlock);
gtime = 1;
pthread_exit(0);
}
void *thread_fun_read2(void*arg)
{
printf("thread read two try to get lock\n");
pthread_rwlock_rdlock(&rwlock);
while(strncmp("end",gdata,3) != 0)
{
printf("this is thread read two. %s \n",gdata);
pthread_rwlock_unlock(&rwlock);
sleep(5);
pthread_rwlock_rdlock(&rwlock);
while(gdata[0] == '\0')
{
pthread_rwlock_unlock(&rwlock);
sleep(5);
pthread_rwlock_rdlock(&rwlock);
}
}
pthread_rwlock_unlock(&rwlock);
gtime = 1;
pthread_exit(0);
}
void *thread_fun_write1(void*arg)
{
printf("this is write thread one try to get lock\n");
while(!gtime)
{
pthread_rwlock_wrlock(&rwlock);
printf("this is write thread one.\n input some text,enter 'end'to finish\n");
fgets(gdata,BUFSIZE,stdin);
pthread_rwlock_unlock(&rwlock);
sleep(15);
}
pthread_rwlock_unlock(&rwlock);
pthread_exit(0);
}
void *thread_fun_write2(void*arg)
{
printf("this is write thread two try to get lock\n");
while(!gtime)
{
pthread_rwlock_wrlock(&rwlock);
printf("this is write thread two.\n input some text,enter 'end'to finish\n");
fgets(gdata,BUFSIZE,stdin);
pthread_rwlock_unlock(&rwlock);
sleep(15);
}
pthread_rwlock_unlock(&rwlock);
pthread_exit(0);
}
阅读全文
0 0
- linux编程---线程---读写锁
- linux系统编程:线程同步-读写锁(rwlock)
- 【Linux系统编程】线程同步与互斥:读写锁
- 线程编程之读写锁
- Linux线程的读写锁
- Linux线程同步-----读写锁
- Linux线程编程 - 线程同步机制之读写锁通信机制
- Linux程序设计学习笔记----多线程编程线程同步机制之互斥量(锁)与读写锁
- linux 系统编程-学习笔记9--线程的同步互斥锁、读写锁/select/poll
- Linux系统编程——线程同步与互斥:读写锁
- Linux系统编程——线程同步与互斥:读写锁
- Linux线程同步(3): 读写锁(rwlock)
- Linux线程同步之读写锁
- Linux线程同步(3): 读写锁(rwlock)
- Linux线程同步(3): 读写锁(rwlock)
- Linux 下的线程读写锁
- Linux 下的线程读写锁
- Linux 下的线程读写锁
- kubernetes--优雅删除资源对象
- [容易] kruskal 算法并查集实现
- JDBC 操作数据库
- jsp include 的flush属性
- Hadoop基础之MapReduce
- linux编程---线程---读写锁
- Maximum_Length_of_Pair_Chain
- Java 开发环境配置
- 使用JavaScript实现对象的深度克隆
- Android OkHttp详细介绍
- vector和list区别【转】
- HashMap链表在Java1.7与1.8中的区别
- azkaban任务延迟执行问题跟进
- POJ2135 Farm Tour —— 最小费用最大流