zoukankan      html  css  js  c++  java
  • Linux IPC 同步(二):读写锁

    读写锁的分配规则如下:

    1.  只要没有线程持有某个指定的读写锁用于写,那么任意数目的线程可以持有该读写锁用于读;

    2.  仅当没有线程持有某个指定的读写锁用于读或者用于写,才能分配读写锁用于写。

    这样的访问方式也称为共享-独占上锁(shared-exclusion)

    那么我想到了这样一个场景:

    线程A要写数据,上锁未果被阻塞,期间不断有读者来读,线程A饿死......  (实验验证之)

    int   pthread_rwlock_init(pthread_rwlock_t *, pthread_rwlockattr_t *);
    int   pthread_rwlock_rdlock(pthread_rwlock_t *);
    int   pthread_rwlock_tryrdlock(pthread_rwlock_t *);
    int   pthread_rwlock_wrlock(pthread_rwlock_t *);
    int   pthread_rwlock_trywrlock(pthread_rwlock_t *);
    int   pthread_rwlock_unlock(pthread_rwlock_t *);
    int   pthread_rwlock_destroy(pthread_rwlock_t *);

    同样:

    读写锁也可以在进程间使用,通过设置属性为PTHREAD_PROCESS_SHARED

    #include <pthread.h>
    int  pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, int *valptr);
    int  pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int value)
  • 相关阅读:
    Oracle Vm VirtualBox 搭建 yum 环境
    Vmware Workstation _linux yum 仓库搭建
    redhat5 设置静态ip
    管理表空间和数据文件
    表空间详解
    ocp linux 基础要点
    事务
    Hash哈希类型
    SortedSet有序集合类型
    set集合类型
  • 原文地址:https://www.cnblogs.com/xiaokuang/p/4619568.html
Copyright © 2011-2022 走看看