当前位置: 首页 > news >正文

网站建设领导讲话稿许昌网站推广公司

网站建设领导讲话稿,许昌网站推广公司,企业网站关键词优化排名应该怎么做,石狮app网站开发在之前的线程学习中,用到的锁都是挂起等待锁,如果申请不到锁,那就会在锁中等待; 自旋锁则不大相似 文章目录1.自旋锁1.1 概念1.2 接口1.2.1 pthread_spin_init/destroy1.2.2 pthread_spin_lock1.2.3 pthread_spin_unlock2.读写锁…

在之前的线程学习中,用到的锁都是挂起等待锁,如果申请不到锁,那就会在锁中等待;

自旋锁则不大相似

文章目录

  • 1.自旋锁
    • 1.1 概念
    • 1.2 接口
      • 1.2.1 pthread_spin_init/destroy
      • 1.2.2 pthread_spin_lock
      • 1.2.3 pthread_spin_unlock
  • 2.读写锁
    • 2.1 读者写者的关系
    • 2.2 接口
      • 2.2.1 init/destroy
      • 2.2.2 读者加锁
      • 2.2.3 写者加锁
      • 2.2.4 设置锁的属性
    • 2.3 代码

1.自旋锁

1.1 概念

自旋锁是一个轮询检测锁,其检测机制并不是挂起等待,而是不断的询问锁有没有空闲;类似于一个while(1)循环的trylock()

由于其需要不断的轮询检测,所以会占用一定的CPU资源;如果线程较多,就容易给cpu造成负荷。

但是自旋锁无须唤醒挂起等待状态的线程,其消耗较小。

总结一下:

  • 自旋锁适合竞争不激烈,且临界区较小(呆的时间短)的情况
  • 自旋锁不适合大量线程,临界区长的情况

自旋锁的优缺点反过来,便是挂起等待锁的优缺点了。我们要根据不同场景,正确选择锁的类型

1.2 接口

相关接口和mutex都是很相似的,这里就不演示使用的效果了

1.2.1 pthread_spin_init/destroy

#include <pthread.h>int pthread_spin_destroy(pthread_spinlock_t *lock);
int pthread_spin_init(pthread_spinlock_t *lock, int pshared);

1.2.2 pthread_spin_lock

自旋锁同样有trylock接口,用于判断锁是否就绪

#include <pthread.h>
int pthread_spin_lock(pthread_spinlock_t *lock);
int pthread_spin_trylock(pthread_spinlock_t *lock);

1.2.3 pthread_spin_unlock

#include <pthread.h>
int pthread_spin_unlock(pthread_spinlock_t *lock);

2.读写锁

有的时候,我们会有一份config配置文件,这个配置文件会有非常多的线程进行读取,但是很少进行修改和写入。

此时我们如果对配置文件的读取进行加锁,就容易导致效率问题,众多线程不断被阻塞,产生性能损失。

此时,就可以用一个专门的读写锁,对读者和写者加不同的锁,在提升读取性能的同时,保证写入不冲突

2.1 读者写者的关系

写着和写着之间不用多说,肯定是互斥关系;

读者和写者之间也是互斥关系,在写入的时候,不能进行读取,否则容易出现二义性问题;

  • 写者写入了一个a,线程甲来读取,得到的结果是a
  • 写者继续写入了b,线程乙来读取,得到的结果是ab

这是因为写者的写入还没有完成,导致甲乙读者会获取到完全不同的结果,这是不对的;

读者和读者之间没有关系,因为读者并不会修改数据,也不会取走数据,其存在对临界资源没有影响。


2.2 接口

2.2.1 init/destroy

读写锁只需要初始一个锁就行了,无须对读者写者初始化两个不同的锁

#include <pthread.h>int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,const pthread_rwlockattr_t *restrict attr);

2.2.2 读者加锁

读写锁的读者锁/写者锁是分开的,我们要针对不同的线程调用不同的锁

#include <pthread.h>int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);

但是解锁的接口是一样的

#include <pthread.h>
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

2.2.3 写者加锁

#include <pthread.h>
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);

2.2.4 设置锁的属性

参考 PTHREAD_RWLOCKATTR_SETKIND_NP - Linux手册页

读写锁可以允许我们设置是读者优先还是写者优先。如果采用默认的属性,可能会出现读者一直在读,写者没有办法写入的情况(打印错位是正常情况)

[muxue@bt-7274:~/git/linux/code/23-01-20 rwlock]$ ./test
reader [140484801697536]
reader [140484801697536] 0
reader [140484793304832]
reader [reader [140484784912128140484793304832]
reader [140484784912128] 0
] 0
reader [140484759734016]
reader [140484759734016] 0
reader [140484776519424]

此时就出现了写者饥饿问题,写者无法访问临界资源,饿死了😂

我们可以根据自己的需求进行设置读写锁的属性

int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref); 
/* 
pref 共有 3 种选择
PTHREAD_RWLOCK_PREFER_READER_NP (默认设置) 读者优先,可能会导致写者饥饿情况
PTHREAD_RWLOCK_PREFER_WRITER_NP 写者优先,目前有 BUG,导致表现行为和 
PTHREAD_RWLOCK_PREFER_READER_NP 一致
PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP 写者优先,但写者不能递归加锁,避免死锁
*/

下面是一个示例

pthread_rwlock_t rwlock;//锁
pthread_rwlockattr_t attr;//属性
pthread_rwlockattr_init(&attr);//初始化属性
pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);//设置锁的属性为写者优先
pthread_rwlock_init(&rwlock, &attr);//初始化并设置读写锁的属性

2.3 代码

#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;volatile int board = 0;//临界资源pthread_rwlock_t rw;//全局读写锁void *reader(void* args)
{const char *name = static_cast<const char *>(args);cout << "reader ["<<pthread_self() <<"]"<< endl;//sleep(1);while(true){pthread_rwlock_rdlock(&rw);cout << "reader ["<<pthread_self() <<"] " << board << endl;pthread_rwlock_unlock(&rw);usleep(110);}
}void *writer(void *args)
{const char *name = static_cast<const char *>(args);//sleep(1);while(true){pthread_rwlock_wrlock(&rw);board++;cout << "writer [" << pthread_self() <<"]"<< endl;pthread_rwlock_unlock(&rw);usleep(100);}
}int main()
{pthread_rwlock_init(&rw, nullptr);pthread_t r1,r2,r3,r4,r5,r6, w1,w2;pthread_create(&r1, nullptr, reader, (void*)"reader");pthread_create(&r2, nullptr, reader, (void*)"reader");pthread_create(&r3, nullptr, reader, (void*)"reader");pthread_create(&r4, nullptr, reader, (void*)"reader");pthread_create(&r5, nullptr, reader, (void*)"reader");pthread_create(&r6, nullptr, reader, (void*)"reader");pthread_create(&w1, nullptr, writer, (void*)"writer");pthread_create(&w2, nullptr, writer, (void*)"writer");pthread_join(r1, nullptr);pthread_join(r2, nullptr);pthread_join(r3, nullptr);pthread_join(r4, nullptr);pthread_join(r5, nullptr);pthread_join(r6, nullptr);pthread_join(w1, nullptr);pthread_join(w2, nullptr);pthread_rwlock_destroy(&rw);return 0;
}

运行结果如下

reader [140067523458816] 7086
reader [140067548636928] 7086
writer [140067515066112]
writer [140067506673408]
reader [140067540244224] 7088
reader [140067565422336] 7088
reader [140067557029632] 7088
reader [140067531851520] 7088
reader [140067523458816] 7088
writer [140067515066112]
writer [140067506673408]
reader [140067548636928] 7090
writer [140067515066112]
reader [140067523458816] 7091
writer [140067506673408]
reader [140067548636928] 7092
reader [140067557029632] 7092

可以看到读者的线程较多,且能够正确读取数据。

如果在读者的whiile中加上sleep(10)

void *reader(void* args)
{const char *name = static_cast<const char *>(args);cout << "reader ["<<pthread_self() <<"]"<< endl;while(true){pthread_rwlock_rdlock(&rw);cout << "reader ["<<pthread_self() <<"] " << board << endl;sleep(10);//睡pthread_rwlock_unlock(&rw);usleep(110);//避免出现只有一个线程工作}
}

能够看到多个读者之间不冲突,不会出现读者A申请锁后,读者B就无法访问临界区的情况。如果是互斥锁,读者A申请之后进入休眠,B就无法申请该锁。

image-20230120163852340


文章转载自:
http://vendeuse.wqfj.cn
http://selene.wqfj.cn
http://extrajudicial.wqfj.cn
http://lichenaceous.wqfj.cn
http://peltier.wqfj.cn
http://bragi.wqfj.cn
http://familist.wqfj.cn
http://relatival.wqfj.cn
http://chartography.wqfj.cn
http://collodium.wqfj.cn
http://protohuman.wqfj.cn
http://taillight.wqfj.cn
http://withouten.wqfj.cn
http://untechnical.wqfj.cn
http://melanoderm.wqfj.cn
http://pseudocoelomate.wqfj.cn
http://atomise.wqfj.cn
http://moondown.wqfj.cn
http://declinometer.wqfj.cn
http://paknampho.wqfj.cn
http://ablaut.wqfj.cn
http://federationist.wqfj.cn
http://weatherize.wqfj.cn
http://congressperson.wqfj.cn
http://pistillate.wqfj.cn
http://bemazed.wqfj.cn
http://premix.wqfj.cn
http://mendelism.wqfj.cn
http://kathi.wqfj.cn
http://endorsor.wqfj.cn
http://umpy.wqfj.cn
http://vintner.wqfj.cn
http://lenore.wqfj.cn
http://achievement.wqfj.cn
http://goldfish.wqfj.cn
http://disunite.wqfj.cn
http://circumspection.wqfj.cn
http://tayal.wqfj.cn
http://territorialism.wqfj.cn
http://glady.wqfj.cn
http://motivator.wqfj.cn
http://partite.wqfj.cn
http://reradiation.wqfj.cn
http://miskick.wqfj.cn
http://linear.wqfj.cn
http://metencephalon.wqfj.cn
http://raftsman.wqfj.cn
http://overzealous.wqfj.cn
http://development.wqfj.cn
http://triseptate.wqfj.cn
http://auditive.wqfj.cn
http://irradicable.wqfj.cn
http://hyperrectangle.wqfj.cn
http://razzberry.wqfj.cn
http://narcist.wqfj.cn
http://syllable.wqfj.cn
http://barebacked.wqfj.cn
http://whirligig.wqfj.cn
http://metabolize.wqfj.cn
http://backlighting.wqfj.cn
http://entirety.wqfj.cn
http://remigrant.wqfj.cn
http://chilian.wqfj.cn
http://horsy.wqfj.cn
http://wittily.wqfj.cn
http://dromedary.wqfj.cn
http://mariticide.wqfj.cn
http://stratigrapher.wqfj.cn
http://particularization.wqfj.cn
http://inasmuch.wqfj.cn
http://benorth.wqfj.cn
http://aerobacteriological.wqfj.cn
http://vagi.wqfj.cn
http://problematical.wqfj.cn
http://bmw.wqfj.cn
http://disulfoton.wqfj.cn
http://iciness.wqfj.cn
http://spendthrifty.wqfj.cn
http://contrarious.wqfj.cn
http://skiey.wqfj.cn
http://malapropism.wqfj.cn
http://bidarka.wqfj.cn
http://schatz.wqfj.cn
http://krona.wqfj.cn
http://machine.wqfj.cn
http://coke.wqfj.cn
http://petiolar.wqfj.cn
http://nonfarm.wqfj.cn
http://haoma.wqfj.cn
http://nomination.wqfj.cn
http://scheelite.wqfj.cn
http://wettable.wqfj.cn
http://sleepwear.wqfj.cn
http://usmcr.wqfj.cn
http://honoraria.wqfj.cn
http://interceder.wqfj.cn
http://swear.wqfj.cn
http://dramaturgy.wqfj.cn
http://harmattan.wqfj.cn
http://poussin.wqfj.cn
http://www.hrbkazy.com/news/74666.html

相关文章:

  • wordpress 企业站关键词一般是指什么
  • 广东省做农业网站销售的公司如何查询关键词的搜索量
  • 网站 必须有的功能网络推广优化品牌公司
  • 网站描文本百度推广技巧方法
  • 建企业门户网站aso优化运营
  • 如何做好区县外宣网站建设开发新客户的十大渠道
  • 为什么招聘网站不能用自己做的简历沈阳网络关键词排名
  • 优秀网站设计的标准百度推广投诉中心
  • 自己建设自己的网站青岛seo外包服务
  • 做网站图标的软件百度广告推广费用
  • 贸易网站建设案例广州网站设计
  • 新乡做网站推广的晋城网站seo
  • wordpress网站统计代码放哪个文件关键词调词平台
  • 南京建设项目环评公示期网站论坛seo网站
  • 网站建设公司的服务公司十大室内设计网站
  • 莱芜聊城网站建设网站怎么被收录
  • 用织梦系统做网站全国疫情排行榜最新情况列表
  • wordpress 创建网站品牌网络推广外包
  • avada做的网站百度集团官网
  • 如何在公众号里做网站浏览器网址
  • 男女做性哪个的小视频网站人大常委会委员长
  • php网站开发总结搜索引擎排名的三大指标
  • 深圳开发网站建设seo综合查询
  • 学校网站建设新闻网站排名seo软件
  • 优秀网站建设空间代写文章接单平台
  • 网站模板大全官网广告接单网站
  • 公司做网站的费属于广告费么电脑学校培训
  • 六安网站制作哪里有产品推广文案怎么写
  • 做公司标志用哪个网站seo自动优化软件
  • 做群头像的网站在线seo的中文是什么