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)
  • 相关阅读:
    Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
    Educational Codeforces Round 60 D dp + 矩阵快速幂
    Educational Codeforces Round 60 C 思维 + 二分
    Codeforces Round #544 (Div. 3) dp + 双指针
    Codeforces Round #542(Div. 2) CDE 思维场
    UVA
    UVA
    UVA
    UVA
    UVA
  • 原文地址:https://www.cnblogs.com/xiaokuang/p/4619568.html
Copyright © 2011-2022 走看看