IPC Read/Write Lock

本文介绍了读写锁的基本概念、函数列表及其应用场景,并通过实例解析了读写锁与互斥锁的区别,强调了读写锁在提高并发度方面的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读写锁

<!--[if !supportLists]-->1、 <!--[endif]-->函数列表

<!--[if !supportLists]-->Ø <!--[endif]-->int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

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

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

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

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

<!--[if !supportLists]-->Ø <!--[endif]-->int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,

const pthread_rwlockattr_t *restrict attr);

PTHREAD_RWLOCK_INITIALIZER

int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

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

销毁读写锁。

<!--[if !supportLists]-->Ø <!--[endif]-->int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);

int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);

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

<!--[if !supportLists]-->Ø <!--[endif]-->int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *

restrict attr, int *restrict pshared);

int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr,

int pshared);

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

<!--[if !supportLists]-->Ø <!--[endif]-->int pthread_cancel(pthread_t thread);

void pthread_cleanup_pop(int execute);

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

线程取消函数

<!--[if !supportLists]-->2、 <!--[endif]-->实例解析

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

//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

<!--[if !supportLists]-->3、 <!--[endif]-->小结

<!--[if !supportLists]-->Ø <!--[endif]-->互斥锁与读写锁的区别

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

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

<!--[if !supportLists]-->Ø <!--[endif]-->读写锁的优点

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

<!--[if !supportLists]-->Ø <!--[endif]-->读写锁描述

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

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

<!--[if !supportLists]-->Ø <!--[endif]-->实现

读写锁可以容易地仅仅使用互斥锁和条件变量来实现,可以优先考虑等待着的写入者,有的实现优先考虑等待着的读出者。【写入锁unlock后,这个锁到底是给另外一个写入者线程,还是另外的读出者线程呢】

内容概要:本文档详细介绍了基于Google Earth Engine (GEE) 构建的阿比让绿地分析仪表盘的设计与实现。首先,定义了研究区域的几何图形并将其可视化。接着,通过云掩膜函数和裁剪操作预处理Sentinel-2遥感影像,筛选出高质量的数据用于后续分析。然后,计算中值图像并提取NDVI(归一化差异植被指数),进而识别绿地及其面积。此外,还实现了多个高级分析功能,如多年变化趋势分析、人口-绿地交叉分析、城市热岛效应分析、生物多样性评估、交通可达性分析、城市扩张分析以及自动生成优化建议等。最后,提供了数据导出、移动端适配和报告生成功能,确保系统的实用性和便捷性。 适合人群:具备一定地理信息系统(GIS)和遥感基础知识的专业人士,如城市规划师、环境科学家、生态学家等。 使用场景及目标:①评估城市绿地分布及其变化趋势;②分析绿地与人口的关系,为城市规划提供依据;③研究城市热岛效应及生物多样性,支持环境保护决策;④评估交通可达性,优化城市交通网络;⑤监测城市扩张情况,辅助土地利用管理。 其他说明:该系统不仅提供了丰富的可视化工具,还集成了多种空间分析方法,能够帮助用户深入理解城市绿地的空间特征及其对环境和社会的影响。同时,系统支持移动端适配,方便随时随地进行分析。用户可以根据实际需求选择不同的分析模块,生成定制化的报告,为城市管理提供科学依据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值