您的位置:首页 > 其它

IPC Read/Write Lock pthread_rwlock_

2012-02-23 11:49 127 查看
读写锁
1、 函数列表

Ø int pthread_rwlock_rdlock(pthread_rwlock_t*rwlock);

intpthread_rwlock_wrlock(pthread_rwlock_t *rwlock);

intpthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);

intpthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_unlock(pthread_rwlock_t*rwlock);

此函数获取一个读出锁,如果对应的读出锁已由某个写入者持有,那就阻塞调用线程;

此函数获取一个写入锁,如果对应的读写锁已由另一个写入者持有,或者已由一个或多个读出者持有,那就阻塞调用线程;

此二函数尝试获取一个读出锁或写入锁,但是如果该锁不能马上取得,那就返回一个EBUSY错误,而不是把调用线程投入睡眠。

此函数对读写锁进行解锁。

Ø intpthread_rwlock_init(pthread_rwlock_t *restrict rwlock,

const pthread_rwlockattr_t *restrictattr);

PTHREAD_RWLOCK_INITIALIZER

int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

动态和静态初始化读写锁;

销毁读写锁。

Ø intpthread_rwlockattr_init(pthread_rwlockattr_t *attr);

intpthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);

初始化和销毁读写锁属性变量。

Ø intpthread_rwlockattr_getpshared(const pthread_rwlockattr_t *

restrict attr, int *restrict pshared);

intpthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr,

int pshared);

获得读写锁属性变量具体值。

Ø int pthread_cancel(pthread_tthread);

void pthread_cleanup_pop(int execute);

void pthread_cleanup_push(void (*routine)(void*), void *arg);

线程取消函数

2、 实例解析

读写锁的基本使用;线程取消的使用。

//testcancel.c

#include <pthread.h>

pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;

pthread_t tid1, tid2;

void *thread1(void *), *thread2(void *);

int

main(int argc, char **argv)

{

void *status;

// Set_concurrency(2);

pthread_create(&tid1, NULL, thread1, NULL);

sleep(1); /* let thread1() get the lock */

pthread_create(&tid2, NULL, thread2, NULL);

pthread_join(tid2, &status);

if (status != PTHREAD_CANCELED)

printf("thread2 status = %p/n", status);

pthread_join(tid1, &status);

if (status != NULL)

printf("thread1 status = %p/n", status);

// printf("rw_refcount = %d, rw_nwaitreaders = %d, rw_nwaitwriters = %d/n",

// rwlock.rw_refcount, rwlock.rw_nwaitreaders,

// rwlock.rw_nwaitwriters);

pthread_rwlock_destroy(&rwlock);

printf("main over/n");

sleep(5);

exit(0);

}

void * thread1(void *arg)

{

pthread_rwlock_rdlock(&rwlock);

printf("thread1() got a read lock/n");

sleep(3); /* let thread2 block in pthread_rwlock_wrlock() */

printf("cancel thread2/n");

pthread_cancel(tid2);

sleep(3);

printf("thread1 unlock rdlock/n");

pthread_rwlock_unlock(&rwlock);

return(NULL);

}

void * thread2(void *arg)

{

printf("thread2() trying to obtain a write lock/n");

pthread_rwlock_wrlock(&rwlock); //发生阻塞,因为已经有读锁

printf("thread2() got2 a write lock/n"); /* should not get here */

sleep(1);

printf("thread2 unlock wrlock/n");

pthread_rwlock_unlock(&rwlock);

return(NULL);

}

//gcc testcancel.c –lpthread –o testcancel

3、 小结

Ø 互斥锁与读写锁的区别

当访问临界区资源时(访问的含义包括所有的操作:读和写),需要上互斥锁;

当对数据(互斥锁中的临界区资源)进行读取时,需要上读取锁,当对数据进行写入时,需要上写入锁。

Ø 读写锁的优点

对于读数据比修改数据频繁的应用,用读写锁代替互斥锁可以提高效率。因为使用互斥锁时,即使是读出数据(相当于操作临界区资源)都要上互斥锁,而采用读写锁,则可以在任一时刻允许多个读出者存在,提高了更高的并发度,同时在某个写入者修改数据期间保护该数据,以免任何其它读出者或写入者的干扰。

Ø 读写锁描述

获取一个读写锁用于读称为共享锁,获取一个读写锁用于写称为独占锁,因此这种对于某个给定资源的共享访问也称为共享-独占上锁。

有关这种类型问题(多个读出者和一个写入者)的其它说法有读出者与写入者问题以及多读出者-单写入者锁。

Ø 实现

读写锁可以容易地仅仅使用互斥锁和条件变量来实现,可以优先考虑等待着的写入者,有的实现优先考虑等待着的读出者。【写入锁unlock后,这个锁到底是给另外一个写入者线程,还是另外的读出者线程呢】
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: