Linux系统多线程读写锁
Linux系统读写锁
实现代码
/*
author : eclipse
email : eclipsecs@qq.com
time : Sat May 9 16:57:24 CST 2020
*/
#include<bits/stdc++.h>
#include<unistd.h>
#include<pthread.h>
using namespace std;pthread_rwlock_t rwlock;
const int READER_NUM = 5;
int value;void* writer(void* arg) {while (true) {pthread_rwlock_wrlock(&rwlock);printf("Writer, write content %d\n", ++value);sleep(2);pthread_rwlock_unlock(&rwlock);}return (void*) 0;
}void* reader(void* arg) {while (true) {pthread_rwlock_rdlock(&rwlock);printf("Reader, read content %d\n", value);sleep(1);pthread_rwlock_unlock(&rwlock);usleep(200);}return (void*) 0;
}int main(int argc, char const * argv[]) {pthread_t readerThreadID[READER_NUM], writerThreadID;pthread_rwlock_init(&rwlock, NULL);for (int i = 0; i < READER_NUM; i++) {if (pthread_create(&readerThreadID[i], NULL, reader, NULL)) {printf("Create thread error!\n");abort();}}if (pthread_create(&writerThreadID, NULL, writer, NULL)) {printf("Create thread error!\n");abort();}for (int i = 0; i < READER_NUM; i++) {pthread_join(readerThreadID[i], NULL);}pthread_join(writerThreadID, NULL);return 0;
}
相关函数原型
pthread_rwlock_init()
- 函数原型
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);
- 参数
- 读写锁对象指针
- 读写锁属性
- 返回值
成功则返回0,否则返回错误代码 - 作用
初始化读写锁
pthread_rwlock_rdlock()
- 函数原型
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
- 作用
读者锁加锁,其他写线程不能获得读写锁
,读进程可以得到读写锁
pthread_rwlock_wrlock()
- 函数原型
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
- 作用
写者锁加锁,其他线程都
不能获得读写锁
pthread_rwlock_unlock()
- 函数原型
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
- 作用
释放读写锁,其他线程可以竞争读写锁
pthread_rwlock_destroy()
- 函数原型
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
- 参数
- 读写锁对象指针
- 返回值
成功则返回0,否则返回错误代码 - 作用
销毁读写锁
注意
编译过程
g++ -pthread pthreadRwlockTest.cpp -o pthreadParameterTest
./pthreadRwlockTest
注释
若注释掉读者函数中的usleep(200);
会导致写者不能执行,读写锁一直被读者线程占用
输出结果
Writer, write content 1
Reader, read content 1
Reader, read content 1
Reader, read content 1
Reader, read content 1
Reader, read content 1
Writer, write content 2
Reader, read content 2
Reader, read content 2
Reader, read content 2
Reader, read content 2
Reader, read content 2
Writer, write content 3
Reader, read content 3
Reader, read content 3
Reader, read content 3
Reader, read content 3
Reader, read content 3
Writer, write content 4
Reader, read content 4
Reader, read content 4
Reader, read content 4
Reader, read content 4
Reader, read content 4
最后
- 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!
Linux系统多线程读写锁
Linux系统读写锁
实现代码
/*
author : eclipse
email : eclipsecs@qq.com
time : Sat May 9 16:57:24 CST 2020
*/
#include<bits/stdc++.h>
#include<unistd.h>
#include<pthread.h>
using namespace std;pthread_rwlock_t rwlock;
const int READER_NUM = 5;
int value;void* writer(void* arg) {while (true) {pthread_rwlock_wrlock(&rwlock);printf("Writer, write content %d\n", ++value);sleep(2);pthread_rwlock_unlock(&rwlock);}return (void*) 0;
}void* reader(void* arg) {while (true) {pthread_rwlock_rdlock(&rwlock);printf("Reader, read content %d\n", value);sleep(1);pthread_rwlock_unlock(&rwlock);usleep(200);}return (void*) 0;
}int main(int argc, char const * argv[]) {pthread_t readerThreadID[READER_NUM], writerThreadID;pthread_rwlock_init(&rwlock, NULL);for (int i = 0; i < READER_NUM; i++) {if (pthread_create(&readerThreadID[i], NULL, reader, NULL)) {printf("Create thread error!\n");abort();}}if (pthread_create(&writerThreadID, NULL, writer, NULL)) {printf("Create thread error!\n");abort();}for (int i = 0; i < READER_NUM; i++) {pthread_join(readerThreadID[i], NULL);}pthread_join(writerThreadID, NULL);return 0;
}
相关函数原型
pthread_rwlock_init()
- 函数原型
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);
- 参数
- 读写锁对象指针
- 读写锁属性
- 返回值
成功则返回0,否则返回错误代码 - 作用
初始化读写锁
pthread_rwlock_rdlock()
- 函数原型
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
- 作用
读者锁加锁,其他写线程不能获得读写锁
,读进程可以得到读写锁
pthread_rwlock_wrlock()
- 函数原型
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
- 作用
写者锁加锁,其他线程都
不能获得读写锁
pthread_rwlock_unlock()
- 函数原型
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
- 作用
释放读写锁,其他线程可以竞争读写锁
pthread_rwlock_destroy()
- 函数原型
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
- 参数
- 读写锁对象指针
- 返回值
成功则返回0,否则返回错误代码 - 作用
销毁读写锁
注意
编译过程
g++ -pthread pthreadRwlockTest.cpp -o pthreadParameterTest
./pthreadRwlockTest
注释
若注释掉读者函数中的usleep(200);
会导致写者不能执行,读写锁一直被读者线程占用
输出结果
Writer, write content 1
Reader, read content 1
Reader, read content 1
Reader, read content 1
Reader, read content 1
Reader, read content 1
Writer, write content 2
Reader, read content 2
Reader, read content 2
Reader, read content 2
Reader, read content 2
Reader, read content 2
Writer, write content 3
Reader, read content 3
Reader, read content 3
Reader, read content 3
Reader, read content 3
Reader, read content 3
Writer, write content 4
Reader, read content 4
Reader, read content 4
Reader, read content 4
Reader, read content 4
Reader, read content 4
最后
- 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!
发布评论