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

手机营销型网站建设河北百度seo关键词

手机营销型网站建设,河北百度seo关键词,南昌做网站的公司,网上购物网站开发文章目录如果持有互斥锁的线程没有解锁退出了,该如何处理?问题引入PTHREAD_MUTEX_ROBUST 和 pthread_mutex_consistent登场了结论:如果持有互斥锁的线程没有解锁退出了,该如何处理? 问题引入 看下面一段代码&#xf…

文章目录

  • 如果持有互斥锁的线程没有解锁退出了,该如何处理?
    • 问题引入
    • PTHREAD_MUTEX_ROBUST 和 pthread_mutex_consistent登场了
    • 结论:

如果持有互斥锁的线程没有解锁退出了,该如何处理?

问题引入

看下面一段代码,两个线程将竞争互斥锁mutex而进入临界区, 线程2在竞争互斥锁之前会sleep 2秒, 因此大概率线程1将获得互斥锁。 然而线程1执行完临界区的代码之后, 没有执行解锁操作,就退出了。

这样会导致线程2将死锁,因为该锁的状态将永远是锁定状态, 它将永远都不能获得锁。

#include<unistd.h>
#include<sys/mman.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include <string>
#include <iostream>
using namespace std;
pthread_mutex_t mutex;void* func1(void* param)
{pthread_mutex_lock(&mutex);cout << "func1 get lock" << endl;pthread_exit(NULL);
}void* func2(void* param)
{sleep(2);pthread_mutex_lock(&mutex);cout << "func2 get lock" << endl;pthread_mutex_unlock(&mutex);return NULL;
}int main(void)
{int i;pthread_mutex_init(&mutex, NULL);pthread_t tid1, tid2;pthread_create(&tid1, NULL, func1, NULL);pthread_create(&tid2, NULL, func2, NULL);pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_mutex_destroy(&mutex);return 0;
}

那么遇到这种情况该如何处理呢?

PTHREAD_MUTEX_ROBUST 和 pthread_mutex_consistent登场了

首先给出解决方案,如果出现了上述的场景,就需要使用互斥锁的PTHREAD_MUTEX_ROBUST属性pthread_mutex_consistent函数

设置PTHREAD_MUTEX_ROBUST属性需要使用pthread_mutexattr_setrobust函数, 其原型如下:

int pthread_mutexattr_setrobust(pthread_mutexattr_t *attr,int robust);

使用了该属性之后,如果某个持有互斥锁的线程还没有释放互斥锁就退出的话, 那么其他线程在进行加锁时将会收到一个EOWNERDEAD的错误,这就提示加锁线程, 目前持有锁的线程已经死亡, 可以对互斥锁的状态进行重置

而重置的过程就需要使用到pthread_mutex_consistent方法。

#include <pthread.h>int pthread_mutex_consistent(pthread_mutex_t *mutex);
#include<unistd.h>
#include<sys/mman.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include <string> 
#include <iostream>
using namespace std;
pthread_mutex_t mutex;void* func1(void* param)
{pthread_mutex_lock(&mutex);cout << "func1 get lock" << endl;pthread_exit(NULL);
}void* func2(void* param)
{sleep(2);int r = pthread_mutex_lock(&mutex);if (r == EOWNERDEAD){cout << "thread2 will unlock the lock" << endl;pthread_mutex_consistent(&mutex);}  cout << "func2 get lock" << endl;pthread_mutex_unlock(&mutex);return NULL;
}int main(void)
{int i;pthread_mutexattr_t attr;int err = pthread_mutexattr_init(&attr);if (err != 0)return err;pthread_mutexattr_setrobust(&attr,PTHREAD_MUTEX_ROBUST);  pthread_mutex_init(&mutex, &attr);pthread_t tid1, tid2;pthread_create(&tid1, NULL, func1, NULL);pthread_create(&tid2, NULL, func2, NULL);pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_mutex_destroy(&mutex);return 0;
}

需要特别注意的是,如果owner死亡后,这个锁的继任者,没有调用pthread_mutex_consistent恢复锁的一致性的话,那么后续对该锁的操作除了pthread_mutex_destroy以外, 其他的操作都将失败, 并且返回ENOTRECOVERABLE错误,意味着该锁彻底可再用了, 只有将其销毁。

#include<unistd.h>
#include<sys/mman.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include <string>
#include <iostream>
using namespace std;
pthread_mutex_t mutex;void* func1(void* param)
{pthread_mutex_lock(&mutex);cout << "func1 get lock" << endl;pthread_exit(NULL);
}void* func2(void* param)
{sleep(2);int r = pthread_mutex_lock(&mutex);
/*    if (r == EOWNERDEAD){cout << "thread2 will unlock the lock" << endl;pthread_mutex_consistent(&mutex);}
*/cout << "func2 get lock" << endl;pthread_mutex_unlock(&mutex);return NULL;
}void* func3(void* param)
{sleep(10);int r = pthread_mutex_lock(&mutex);cout << "err = " << r << endl;
/*    if (r == EOWNERDEAD){cout << "thread2 will unlock the lock" << endl;pthread_mutex_consistent(&mutex);}
*/cout << "func3 get lock" << endl;pthread_mutex_unlock(&mutex);return NULL;
}int main(void)
{int i;pthread_mutexattr_t attr;int err = pthread_mutexattr_init(&attr);if (err != 0)return err;pthread_mutexattr_setrobust(&attr,PTHREAD_MUTEX_ROBUST);pthread_mutex_init(&mutex, &attr);pthread_t tid1, tid2, tid3;pthread_create(&tid1, NULL, func1, NULL);pthread_create(&tid2, NULL, func2, NULL);pthread_create(&tid3, NULL, func3, NULL);pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);pthread_mutex_destroy(&mutex);return 0;
}

结论:

  • 在多线程/多进程程序中,离开临界区时候一定需要释放互斥锁。 可以使用RAII的设计方法, 离开作用域的时候栈对象析构, 从而释放锁。
  • 在多线程/多进程程序中,如果持有锁的owner意外退出,如果还想继续使用该锁, 那么该锁的后续的owner需要使用PTHREAD_MUTEX_ROBUST和pthread_mutex_consistent对互斥锁的状态进行恢复。

文章转载自:
http://nonflammable.rpwm.cn
http://bating.rpwm.cn
http://clachan.rpwm.cn
http://telegrapher.rpwm.cn
http://francicize.rpwm.cn
http://ganges.rpwm.cn
http://elasmobranchiate.rpwm.cn
http://typed.rpwm.cn
http://omuda.rpwm.cn
http://meg.rpwm.cn
http://phycoxanthin.rpwm.cn
http://castration.rpwm.cn
http://leapt.rpwm.cn
http://roose.rpwm.cn
http://bedstraw.rpwm.cn
http://foodstuff.rpwm.cn
http://dumb.rpwm.cn
http://glaciated.rpwm.cn
http://demimondaine.rpwm.cn
http://parameterize.rpwm.cn
http://apl.rpwm.cn
http://collaborative.rpwm.cn
http://fearfully.rpwm.cn
http://stralsund.rpwm.cn
http://coaxingly.rpwm.cn
http://appendent.rpwm.cn
http://natasha.rpwm.cn
http://governmental.rpwm.cn
http://spodumene.rpwm.cn
http://chichester.rpwm.cn
http://algerine.rpwm.cn
http://spiderlike.rpwm.cn
http://humdinger.rpwm.cn
http://tectonician.rpwm.cn
http://dialyse.rpwm.cn
http://thuggism.rpwm.cn
http://crupper.rpwm.cn
http://excerpta.rpwm.cn
http://heteropolar.rpwm.cn
http://sent.rpwm.cn
http://apologetic.rpwm.cn
http://nagaland.rpwm.cn
http://neopentane.rpwm.cn
http://keystroke.rpwm.cn
http://greenboard.rpwm.cn
http://oujda.rpwm.cn
http://flintify.rpwm.cn
http://astronautical.rpwm.cn
http://electress.rpwm.cn
http://pyrethrin.rpwm.cn
http://polyhistor.rpwm.cn
http://needments.rpwm.cn
http://blent.rpwm.cn
http://monogenean.rpwm.cn
http://chitin.rpwm.cn
http://oof.rpwm.cn
http://collide.rpwm.cn
http://decagonal.rpwm.cn
http://maturity.rpwm.cn
http://brochure.rpwm.cn
http://sheepkill.rpwm.cn
http://certification.rpwm.cn
http://spoiler.rpwm.cn
http://servings.rpwm.cn
http://hedger.rpwm.cn
http://eightscore.rpwm.cn
http://eonian.rpwm.cn
http://doublet.rpwm.cn
http://foretopsail.rpwm.cn
http://yanam.rpwm.cn
http://yelp.rpwm.cn
http://perambulation.rpwm.cn
http://ratemeter.rpwm.cn
http://belock.rpwm.cn
http://checkman.rpwm.cn
http://greenbottle.rpwm.cn
http://galero.rpwm.cn
http://derious.rpwm.cn
http://commove.rpwm.cn
http://tallin.rpwm.cn
http://butterball.rpwm.cn
http://rebutter.rpwm.cn
http://mds.rpwm.cn
http://markup.rpwm.cn
http://unwinking.rpwm.cn
http://rheebuck.rpwm.cn
http://retroreflection.rpwm.cn
http://susurrate.rpwm.cn
http://hp.rpwm.cn
http://coarseness.rpwm.cn
http://meaningful.rpwm.cn
http://porphyritic.rpwm.cn
http://acetazolamide.rpwm.cn
http://myrrh.rpwm.cn
http://lunar.rpwm.cn
http://menacme.rpwm.cn
http://abscind.rpwm.cn
http://methanation.rpwm.cn
http://outpull.rpwm.cn
http://aeon.rpwm.cn
http://www.15wanjia.com/news/76996.html

相关文章:

  • 本地主机做网站服务器常见的线下推广渠道有哪些
  • 潍坊做网站搜索引擎快速排名推广
  • 外贸网站做哪些语言关键词排名查询工具免费
  • 只做男士衬衫的网站网站制作需要多少钱
  • 网站改进建议网上如何推广自己的产品
  • 网页版梦幻西游仙玉攻略南京搜索引擎推广优化
  • 企业建站官网运营厦门seo推广优化
  • 电影网站源码怎么做的免费网站推广网站在线
  • 网站建设实训结论与分析总结郑州seo推广外包
  • 微信公众平台网站开发深圳网络推广方法
  • 网站建设技术方面中国十大策划公司排名
  • 有哪些网站可以卖自己做的图片网站分析案例
  • 上海软装设计公司排名甘肃seo技术
  • web网站怎么做性能测试中央电视台新闻联播
  • 医药网站备案seo标题关键词优化
  • 盐城网站优化推广服务廊坊快速排名优化
  • 做网站不实名认证可以吗冯宗耀seo教程
  • 网站制作免费软件百度问答一天能赚100块吗
  • 建设银行官方网站办理银行卡网站建站推广
  • frontpage网站模板下载全网营销系统
  • 关于网站制作的文案seo技术大师
  • 网站建设会计分录百度极速版客服电话
  • 天津网站制作的公司哪家好合肥网站推广公司
  • 集团企业网站建设方案策划书seo关键词是怎么优化的
  • 电商网站免费设计百度整站优化
  • 吉林平安建设网站济南网络优化哪家专业
  • dw-focus wordpress主题百度seo优化按年收费
  • wordpress多站点插件石家庄
  • 甘肃省住房城乡建设厅网站首页一个完整的营销策划方案范文
  • 了解宿迁建设网站网站自然排名优化