app开发者平台在数字化时代的重要性与发展趋势解析
606
2022-11-09
进程通信之读写锁
读写锁
读写锁的分配规则 1. 没有线程持有读写锁进行写,任意数量的线程可以持有该读写锁用于读 2. 只有没有线程持有给定的读写锁用于读或者写的时候,才能分配读写锁用于写。 如果修改数据频繁,那么可以考虑用读写锁替代互斥锁。
获取与释放
如果对应的读写锁已由某个写入者持有,那么阻塞pthread_rwlock_rdlock获取读出锁如果对应的读写锁已由另一个写入者持有,那就阻塞pthread_rwlock_wrlock获取写入锁。pthread_rwlock_unlock用于释放读出锁或者写入锁。三者成功时返回0,出错时返回正的错误值pthread_rwlock_tryrdlock(pthread _rwlock_t *rwptr)尝试获取读出锁,如果不能马上获得,返回EBUSY错误。pthread_rwlock_trywrlock(pthread _rwlock_t *rwptr)尝试获取写入锁,如果不能马上获得,返回EBUSY错误。
初始化和摧毁
动态初始化int pthread_rwlock_init(pthread_rwlock_t *rwptr, const pthread_rwlockattr_t *attr)静态初始化PTHREAD_RWLOCK_INITIALIZER摧毁int pthread_rwlock_destroy(pthread_rwlock_t *rwptr)
属性设置
int pthread_rwlockattr_init(pthread_rwlockattr_t * attr)int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t * attr,int *valptr)int pthread_rwlockattr_setpshared(pthread_rwlockattr_t * attr,int
读写锁很适合读的次数远大于写的次数的情况。 例子: 一个写者,多个读者。用读写锁进行协调工作。 code:
#include
运行:
writer 1 is writing!finish, wrote it.reader 2 is reading!data: number is 1, info are I love linux..writer 1 is writing!the pdata is not null.reader 2 is reading!data: number is 1, info are I love linux..reader 2 is reading!data: number is 1, info are I love linux..
线程取消函数: int pthread_cancel(pthread_t tid) 一个线程可以被同一进程中的其他线程取消。 与之对应的自愿终止: void pthread_exit(void *retval)
#include
./pthread_cancel thread1 got a read lock.thread2 try to obtain a write lock.thread2() get a write
fcntl函数和读写锁
文件锁:建议性锁,强制性锁 lock结构体:
Struct flock{short l_type;off_t l_start;short
flock可施加建议性锁,fcntl既可以施加建议性锁,也可施加强制性锁。 fcntl也可以对某一记录进行上锁,也就是所谓的记录锁。记录锁分为读取锁(共享锁),写入锁(排斥锁,互斥锁)。 fcntl建立记录锁: 下面是一个学习的例子,参考 使用fcntl增加和释放锁:
#include
不修改上面的代码,试试写入锁 + 写入锁, 看看效果。 在不同的标签内运行此程序:
write + write
./writewrite lock by 2904wait for a secend...release lock by 2904wait for a secend..../writewrite lock already set by 2904please enter any key to continue: write lock already set by 2904please enter any key to continue: write lock by 2905wait for a secend...release lock by 2905wait for
在已有write锁的情况下不能加上write锁。 现在,稍作修改
lock_set(fd,F_WRLCK);--->lock_set(fd,F_RDLCK);
write + read
./write write lock by 3005wait for a secend...release lock by 3005wait for a secend..../read write lock already set by 3005please enter any key to continue: write lock already set by 3005please enter any key to continue: write lock already set by 3005please enter any key to continue: read lock by 3006wait for a secend...release lock by 3006wait for
在已有write锁的情况下不能加上read锁
read + write
./read read lock by 3019wait for a secend...release lock by 3019wait for a secend..../write read lock already set by 3019please enter any key to continue: read lock already set by 3019please enter any key to continue: read lock already set by 3019please enter any key to continue: read lock already set by 3019please enter any key to continue: write lock by 3020wait for a secend...release lock by 3020wait for
在已有read锁的情况下不能加上write锁
read + read
./readread lock by 3035wait for a secend...release lock by 3035wait for a secend..../read read lock by 3036wait for a secend...release lock by 3036wait for a
read锁和read锁不冲突。在已有read锁的情况下可以加上read锁。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~