特性:
读写锁也叫共享——排他锁,因为有3种状态, 所以可以有更高的并行性。使用mutex,它的状态要么处于锁住和未锁状态,只有一个线程可以上锁。而读写锁有更多的状态:在读状态锁住,在写状态锁住,未锁住。只有一个线程可以获得写锁,多个线程可以同时获得读锁。
• 当读写锁是写加锁状态时, 在这个锁被解锁之前, 所有试图对这个锁加锁的线程都会被阻塞。
• 当读写锁在读加锁状态时, 所有试图以读模式对它进行加锁的线程都可以得到访问权, 但是如果线程希望以写模式对此锁进行加锁, 它必须阻塞知道所有的线程释放锁。
• 通常, 当读写锁处于读模式锁住状态时, 如果有另外线程试图以写模式加锁, 读写锁通常会阻塞随后的读模式锁请求, 这样可以避免读模式锁长期占用, 而等待的写模式锁请求长期阻塞。
适用性:
读写锁适合读比写频繁情形。读写锁和互斥量一样也需要在使用前初始化,在释放他们内存的时候销毁。
初始化和销毁:
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);
int pthread_rwlock_destroy(pthread_rwlock_t *restrict rwlock);
一个读写锁可以调用pthread_rwlock_init来初始化,我们可以传递NULL作为attr的参数,这样会使用读写锁的默认属性。我们可以调用pthread_rwlock_destroy来清理,销毁它所占的内存空间。
读和写:
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
实现上可能会对读写锁中读模式的锁锁住次数有一定的限制,所以我们需要检查返回值,以确定是否成功。而其他的两个函数会返回错误,但是只要我们的锁设计的恰当,我们可以不必做检查。
非阻塞的函数为:
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
当锁成功获取时,返回0,否则返回EBUSY。这两个函数可以避免死锁。
如果针对未初始化的读写锁调用进行读写操作,则结果是不确定的。
释放:
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
用来释放在 rwlock 引用的读写锁对象中持有的锁。
如果调用线程未持有读写锁 rwlock,或者针对未初始化的读写锁调用该函数,则结果是不确定的。
例子:
#define _XOPEN_SOURCE 500
#include <pthread.h>
#define PTHREAD_RWLOCK_INITIALIZER_READ_PREF { {0, 0}, 0, NULL, NULL, NULL, PTHREAD_RWLOCK_PREFER_READER_NP, PTHREAD_PROCESS_PRIVATE }
static pthread_rwlock_t a = PTHREAD_RWLOCK_INITIALIZER;
void *route_3 (void *p)
{
sleep(2);
printf("locking 3 = %d\n", pthread_rwlock_rdlock(&a));
pause();
return NULL;
}
void *route_2 (void *p)
{
sleep(1);
printf("locking 2 = %d\n", pthread_rwlock_wrlock(&a));
pause();
return NULL;
}
void *route_1 (void *p)
{
printf("locking 1 = %d\n", pthread_rwlock_rdlock(&a));
pause();
return NULL;
}
main()
{
pthread_t t1, t2, t3;
pthread_create(&t1, NULL, route_1, NULL);
pthread_create(&t2, NULL, route_2, NULL);
pthread_create(&t3, NULL, route_3, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
}
#include <errno.h>
#include <pthread.h>
static pthread_rwlock_t listlock;
static int lockiniterror = 0;
static pthread_once_t lockisinitialized = PTHREAD_ONCE_INIT;
static void ilock(void) {
lockiniterror = pthread_rwlock_init(&listlock, NULL);
}
int initialize_r(void) { /* must be called at least once before using list */
if (pthread_once(&lockisinitialized, ilock))
lockiniterror = EINVAL;
return lockiniterror;
}
int accessdata_r(void) { /* get a nonnegative key if successful */
int error;
int errorkey = 0;
int key;
if (error = pthread_rwlock_wrlock(&listlock)) { /* no write lock, give up */
errno = error;
return -1;
}
key = accessdata();
if (key == -1) {
errorkey = errno;
pthread_rwlock_unlock(&listlock);
errno = errorkey;
return -1;
}
if (error = pthread_rwlock_unlock(&listlock)) {
errno = error;
return -1;
}
return key;
}
int adddata_r(data_t data) { /* allocate a node on list to hold data */
int error;
if (error = pthread_rwlock_wrlock(&listlock)) { /* no writer lock, give up */
errno = error;
return -1;
}
if (adddata(data) == -1) {
error = errno;
pthread_rwlock_unlock(&listlock);
errno = error;
return -1;
}
if (error = pthread_rwlock_unlock(&listlock)) {
errno = error;
return -1;
}
return 0;
}
int getdata_r(int key, data_t *datap) { /* retrieve node by key */
int error;
if (error = pthread_rwlock_rdlock(&listlock)) { /* no reader lock, give up */
errno = error;
return -1;
}
if (getdata(key, datap) == -1) {
error = errno;
pthread_rwlock_unlock(&listlock);
errno = error;
return -1;
}
if (error = pthread_rwlock_unlock(&listlock)) {
errno = error;
return -1;
}
return 0;
}
int freekey_r(int key) { /* free the key */
int error;
if (error = pthread_rwlock_wrlock(&listlock)) {
errno = error;
return -1;
}
if (freekey(key) == -1) {
error = errno;
pthread_rwlock_unlock(&listlock);
errno = error;
return -1;
}
if (error = pthread_rwlock_unlock(&listlock)) {
errno = error;
return -1;
}
return 0;
}
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h> //文件状态结构
#include <unistd.h>
#include <sys/mman.h> //mmap头文件
#define BSIZE 10
typedef struct {
char buf[BSIZE];
int occupied;
int nextin;
int nextout;
pthread_mutex_t mutex;
pthread_cond_t more;
pthread_cond_t less;
} buffer_t;
char consumer(buffer_t *b)
{
char item;
pthread_mutex_lock(&b->mutex);
while(b->occupied <= 0)
pthread_cond_wait(&b->more, &b->mutex);
assert(b->occupied > 0);
item = b->buf[b->nextout++];
b->nextout %= BSIZE;
b->occupied--;
/* now: either b->occupied > 0 and b->nextout is the index
of the next occupied slot in the buffer, or
b->occupied == 0 and b->nextout is the index of the next
(empty) slot that will be filled by a producer (such as
使用条件变量
120 多线程编程指南• 2006年10月
示例4–13 生成方和使用者问题:使用者(续)
b->nextout == b->nextin) */
pthread_cond_signal(&b->less);
pthread_mutex_unlock(&b->mutex);
return(item);
}
void producer(buffer_t *b, char item)
{
pthread_mutex_lock(&b->mutex);
while (b->occupied >= BSIZE)
pthread_cond_wait(&b->less, &b->mutex);
assert(b->occupied < BSIZE);
b->buf[b->nextin++] = item;
b->nextin %= BSIZE;
b->occupied++;
/* now: either b->occupied < BSIZE and b->nextin is the index
of the next empty slot in the buffer, or
b->occupied == BSIZE and b->nextin is the index of the
next (occupied) slot that will be emptied by a consumer
(such as b->nextin == b->nextout) */
pthread_cond_signal(&b->more);
pthread_mutex_unlock(&b->mutex);
}
void producer_driver(buffer_t *b) {
int item;
while (1) {
item = getchar();
if (item == EOF) {
producer(b, ‘\0’);
break;
} else
producer(b, (char)item);
}
return 0
}
void consumer_driver(buffer_t *b) {
char item;
while (1) {
if ((item = consumer(b)) == ’\0’)
break;
putchar(item);
}
}
int main() {
int zfd;
buffer_t *buffer;
pthread_mutexattr_t mattr;
pthread_condattr_t cvattr_less, cvattr_more;
zfd = open("/dev/zero", O_RDWR);
buffer = (buffer_t *)mmap(NULL, sizeof(buffer_t),PROT_READ|PROT_WRITE, MAP_SHARED, zfd, 0);
buffer->occupied = buffer->nextin = buffer->nextout = 0;
pthread_mutex_attr_init(&mattr);
pthread_mutexattr_setpshared(&mattr,PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&buffer->lock, &mattr);
pthread_condattr_init(&cvattr_less);
pthread_condattr_setpshared(&cvattr_less, PTHREAD_PROCESS_SHARED);
pthread_cond_init(&buffer->less, &cvattr_less);
pthread_condattr_init(&cvattr_more);
pthread_condattr_setpshared(&cvattr_more,PTHREAD_PROCESS_SHARED);
pthread_cond_init(&buffer->more, &cvattr_more);
if (fork() == 0)
consumer_driver(buffer);
else
producer_driver(buffer);
}
分享到:
相关推荐
windows和linux读写锁C++实现
inux 读写锁应用实例 /*使用读写锁实现四个线程读写一段程序的实例,共创建了四个新的线程,其中两个线程用来读取数据,另外两个线程用来写入数据。在任意时刻,如果有一个线程在写数据,将阻塞所有其他线程的任何...
针对之前版本的windows/linux读写锁的简单改进版本,提高某些情况下的加锁效率。
linux文件读写锁一个例子,已经通过测试,使用非常方便
在linux下按照windows的slim read/write lock算法实现的读写锁源码。
在linux下有两种实现数据互斥的基本机制,包括了信号量,自旋锁。这里要说的读写锁是自旋锁的一个变种
linux下用标准C写的线程池全部实现代码,其中包含有读写锁的实现和应用
Linux系统编程——线程同步与互斥:读写锁,相关教程链接如下: http://blog.csdn.net/tennysonsky/article/details/46485735
互斥锁通信机制 条件变量通信机制 读写锁通信机制 线程与信号
说明:编译时加上参数 -lrt -lpthread 要不然找不到库文件
本文档是关于Linux读写锁的讲义,想要学好操作系统的可以来看看
3. 读写锁是“读模式加锁”时, 既有试图以写模式加锁的线程,也有试图以读模式加锁的线程。那么读写锁会阻塞随后的读模式锁请求。优先满足写模式锁。读锁、写锁并行阻塞,写锁优先级高 读写锁也叫共享-独占锁。当...
在哪种系统下都会有文件操作产生的同步问题,今天说说linux下读写文件的锁机制。
【linux学习笔记-6】文件IO操作--读写、重定向等 【linux学习笔记-7】用户标识UID与有效用户标识EUID 【linux学习笔记-8】Linux进程相关系统调用(1) 【linux学习笔记-9】Linux进程相关系统调用(2) 【linux学习...
**读写锁总结:** 1. 读写锁分为读锁和写锁。 2. 如果资源被读写锁保护,多个线程可以同时获取读锁—也就是读支持多个线程同时读。 3. 资源加了写锁之后,在写资源的时候只能被一个线程占用,其他读锁就会阻塞。 ...
【linux学习笔记-13】基本进程通信--文件锁.doc 【linux学习笔记-14】基本进程通信--信号.doc 【linux学习笔记-15】基本进程通信--管道.doc 【linux学习笔记-16】shell管道重定向程序的实现.doc 【linux学习笔记-1】...
共享锁也称为读写锁,其特点是读和读不互斥,读写互斥,写写互斥。在多线程开发中一般使用在多个读单个写入的场景,比如将摄像头的画面读取后显示并同时编码推流。c语言做跨平台开发,实现一个共享锁封装对象还是...
简单的linux多线程实现,含互斥锁的使用
详细介绍了linux系统下编程环境及编程工具、文件管理(文件类型、ansi以及posix标准下文件读写操作)、进程管理(创建、退出、执行、等待、属性控制)、进程间通信(管道、消息队列、共享内存)、进程间同步机制(信号量)、...
详细介绍了linux系统下编程环境及编程工具、文件管理(文件类型、ansi以及posix标准下文件读写操作)、进程管理(创建、退出、执行、等待、属性控制)、进程间通信(管道、消息队列、共享内存)、进程间同步机制(信号量)、...