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

网站赢利石家庄网站建设方案

网站赢利,石家庄网站建设方案,网站稳定性不好的原因,企业管理咨询服务合同范本文章目录 📝线程互斥🌠 库函数strncpy🌉进程线程间的互斥相关背景概念🌉互斥量mutex 🌠线程同步🌉条件变量🌉同步概念与竞态条件🌉 条件变量函数 🚩总结 📝线…

请添加图片描述

文章目录

  • 📝线程互斥
  • 🌠 库函数strncpy
    • 🌉进程线程间的互斥相关背景概念
    • 🌉互斥量mutex
  • 🌠线程同步
    • 🌉条件变量
    • 🌉同步概念与竞态条件
    • 🌉 条件变量函数
  • 🚩总结


📝线程互斥

🌠 库函数strncpy

🌉进程线程间的互斥相关背景概念

  • 临界资源:多线程执⾏流共享的资源就叫做临界资源
  • 临界区:每个线程内部,访问临界资源的代码,就叫做临界区
  • 互斥:任何时刻,互斥保证有且只有⼀个执⾏流进⼊临界区,访问临界资源,通常对临界资源起保护作⽤
  • 原⼦性(后⾯讨论如何实现):不会被任何调度机制打断的操作,该操作只有两态,要么完成,要么未完成

🌉互斥量mutex

  • ⼤部分情况,线程使⽤的数据都是局部变量,变量的地址空间在线程栈空间内,这种情况,变量归属单个线程,其他线程⽆法获得这种变量。
  • 但有时候,很多变量都需要在线程间共享,这样的变量称为共享变量,可以通过数据的共享,完成线程之间的交互。
  • 多个线程并发的操作共享变量,会带来⼀些问题。

Makefile文件

bin=ticket
cc=g++
src=$(wildcard *.cc)
obj=$(src:.cc=.o)$(bin):$(obj)$(cc) -o $@ $^ -lpthread
%.o:%.cc@echo "Comiling $< to $@"$(cc) -c $< -std=c++17.PHONY:clean
clean:rm -f $(bin) $(obj).PHONY:test
test:echo $(src)echo $(obj)

代码:

#include <stdio.h>
#include <string>
#include <string.h>
#include <pthread.h>
#include <unistd.h>int ticket = 100;void* routine(void* args)
{char *id = (char*)args;// std::string id = static_cast<const char*>(args);while(1){if(ticket > 0){usleep(10000);printf("%s sells ticket:%d\n", id, ticket);ticket--;}else{break;}}return nullptr;
}int main(void)
{pthread_t t1, t2 , t3, t4;pthread_create(&t1, nullptr, routine, (void*)"thread 1");pthread_create(&t2, nullptr, routine, (void*)"thread 2");pthread_create(&t3, nullptr, routine, (void*)"thread 3");pthread_create(&t4, nullptr, routine, (void*)"thread 4");pthread_join(t1, nullptr);pthread_join(t2, nullptr);pthread_join(t3, nullptr);pthread_join(t4, nullptr);return 0;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • if语句判断条件为真以后,代码可以并发的切换到其他进程
  • usleep这个模拟夜漫长业务的过程这个漫长的业务过程中,可能有多个线程会进入该代码段
  • --ticket操作本身就不是一个原子操作

取出ticket–部分的汇编代码

objdump -d a.out > test.objdump152   40064b:   8b 05 e3 04 20 00     mov       0x2004e3(%rip),%eax 
600b34 <ticket>153   400651:   83 e8 01                	 sub        $0x1,%eax
154   400654:   89 05 da 04 20 00       mov		%eax,0x2004da(%rip)  
600b34 <ticket>

--操作并不是原子操作而是对应三条汇编指令:

  • load将共享变量体的从内存加载到寄存器
  • update更新寄存器里面的只执行复议操作
  • store:将新值从寄存器写回共享变量ticket的内存地址

要解决以上问题需要做到三点:

  • 代码必须要有互斥行为:当代码进入临界区执行时,不允许其他进程进入该临界区
  • 如果多个线程同时要求进入临界区的代码,并且临界区没有线程在执行,那么只能一个线程进入该临界区
  • 如果现场不在临界区中执行,那么该现场就不能阻止其他进程进入临界区

要做到这三点,本身是上就是需要一把锁,linux上提供这把锁叫互斥量
在这里插入图片描述
互斥量的接口
初始化互斥量的两种方法

  • 方法1:静态分配
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER 
  • ⽅法2,动态分配:
    int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
    参数:
    mutex:要初始化的互斥量
    attr:这是一个指向 pthread_mutexattr_t 类型对象的指针,该类型用于定义互斥锁的属性。如果将其设置为 NULL

销毁互斥量
销毁互斥量需要注意:

  • 使用PTHREAD_ MUTEX_ INITIALIZER初始化的互斥量不需要销毁
  • 不要销毁⼀个已经加锁的互斥量
  • 已经销毁的互斥量,要确保后⾯不会有线程再尝试加锁
int pthread_mutex_destroy(pthread_mutex_t *mutex);

互斥量加锁和解锁

int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
返回值:成功返回o,失败返回错误号

用pthread_ lock时,可能会遇到以下情况:

  • 互斥量处于未锁状态,该函数会将互斥量锁定,同时返回成功
  • 发起函数调用时,其他线程已经锁定互斥量,或者存在其他线程同时申请互斥量,但没有竞争到互斥量,那么pthread_lock调用会陷入阻塞(执行流被挂起),等待互斥量解锁。

改进上面的售票系统:

#include <stdio.h>
#include <string>
#include <string.h>
#include <pthread.h>
#include <unistd.h>int ticket = 100;
pthread_mutex_t mutex;void* routine(void* args)
{char *id = (char*)args;// std::string id = static_cast<const char*>(args);while(1){pthread_mutex_lock(&mutex);if(ticket > 0){usleep(1000);printf("%s sells ticket:%d\n", id, ticket);ticket--;pthread_mutex_unlock(&mutex);}else{pthread_mutex_unlock(&mutex);break;}}return nullptr;
}int main(void)
{pthread_t t1, t2 , t3, t4;pthread_create(&t1, nullptr, routine, (void*)"thread 1");pthread_create(&t2, nullptr, routine, (void*)"thread 2");pthread_create(&t3, nullptr, routine, (void*)"thread 3");pthread_create(&t4, nullptr, routine, (void*)"thread 4");pthread_join(t1, nullptr);pthread_join(t2, nullptr);pthread_join(t3, nullptr);pthread_join(t4, nullptr);pthread_mutex_destroy(&mutex);return 0;
}

在这里插入图片描述
在这里插入图片描述

RAII风格的互斥锁,C++11也有,比如:
std : : mutex mtx;
std : : lock_guard guard ( mtx ) ;

🌠线程同步

🌉条件变量

  • 当⼀个线程互斥地访问某个变量时,它可能发现在其它线程改变状态之前,它什么也做不了。
  • 例如⼀个线程访问队列时,发现队列为空,它只能等待,只到其它线程将⼀个节点添加到队列中。这种情况就需要⽤到条件变量。

🌉同步概念与竞态条件

  • 同步:在保证数据安全的前提下,让线程能够按照某种特定的顺序访问临界资源,从⽽有效避免饥饿问题,叫做同步
  • 竞态条件:因为时序问题,⽽导致程序异常,我们称之为竞态条件。在线程场景下,这种问题也
    不难理解

🌉 条件变量函数

初始化

int pthread_cond_init(pthread_cond_t *restrict cond,   const pthread_condattr_t*restrict attr);

参数:
cond:要初始化的条件变量
attr: NULL

销毁:

int pthread_cond_destroy(pthread_cond_t *cond)

等待条件满⾜

int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrictmutex);
参数:
cond:要在这个条件变量上等待
mutex:互斥量,后面详细解释

唤醒等待

int pthread_cond_broadcast(pthread_cond_t *cond);
int pthread_cond_signal(pthread_cond_t *cond);

简单案例:

  • 我们先使用PTHREAD_COND/MUTEX_INITIALIZER进行测试,对其他细节暂不追究
  • 然后将接口更改成为使用pthread_cond_init/pthread_cond_destroy的方式,方便后续进行封装
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <pthread.h>pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;void* active(void* args)
{std::string name = static_cast<const char*>(args);while(true){pthread_mutex_lock(&mutex);pthread_cond_wait(&cond, &mutex);std::cout<< name << "活动..." << std::endl;pthread_mutex_unlock(&mutex);}
}int main()
{pthread_t t1, t2;pthread_create(&t1, nullptr, active, (void*)"thread -1");pthread_create(&t2, nullptr, active, (void*)"thread -2");sleep(3);while(true){//对比测试pthread_cond_signal(&cond);//唤醒一个线程// pthread_cond_broadcast(&cond);//唤醒所有线程sleep(1);}pthread_join(t1, nullptr);pthread_join(t2, nullptr);pthread_cond_destroy(&cond);pthread_mutex_destroy(&mutex);return 0;
}

在这里插入图片描述


🚩总结

请添加图片描述


文章转载自:
http://dinncoentrechat.tpps.cn
http://dinncovolcanologist.tpps.cn
http://dinncohydrolant.tpps.cn
http://dinncohepatize.tpps.cn
http://dinncounspeak.tpps.cn
http://dinncorevisionist.tpps.cn
http://dinncoganglion.tpps.cn
http://dinncocallee.tpps.cn
http://dinncohypomnesia.tpps.cn
http://dinncorabbath.tpps.cn
http://dinncononearthly.tpps.cn
http://dinncomarksman.tpps.cn
http://dinncogendarme.tpps.cn
http://dinncomahratti.tpps.cn
http://dinncosubtlety.tpps.cn
http://dinncoechinococcosis.tpps.cn
http://dinncooverbowed.tpps.cn
http://dinncogreenwing.tpps.cn
http://dinncosalicional.tpps.cn
http://dinncoexpressionist.tpps.cn
http://dinncoherma.tpps.cn
http://dinncohemospasia.tpps.cn
http://dinncomop.tpps.cn
http://dinncogalatea.tpps.cn
http://dinncozymotechnics.tpps.cn
http://dinncoikbal.tpps.cn
http://dinncorelate.tpps.cn
http://dinncoperfoliate.tpps.cn
http://dinncodecadal.tpps.cn
http://dinnconought.tpps.cn
http://dinncotemperable.tpps.cn
http://dinncocastroite.tpps.cn
http://dinncosemidomestic.tpps.cn
http://dinncosignalise.tpps.cn
http://dinncomidgarth.tpps.cn
http://dinncospiritually.tpps.cn
http://dinncoprepotent.tpps.cn
http://dinncoproletariate.tpps.cn
http://dinnconiff.tpps.cn
http://dinncorallymaster.tpps.cn
http://dinncopacesetting.tpps.cn
http://dinncohippomenes.tpps.cn
http://dinncocorean.tpps.cn
http://dinncoquenchable.tpps.cn
http://dinncomaidless.tpps.cn
http://dinncoabhor.tpps.cn
http://dinncobudge.tpps.cn
http://dinncosemifascist.tpps.cn
http://dinncovulcanic.tpps.cn
http://dinncogunship.tpps.cn
http://dinncofrostline.tpps.cn
http://dinncogirth.tpps.cn
http://dinncocadet.tpps.cn
http://dinncosiriasis.tpps.cn
http://dinncohajji.tpps.cn
http://dinncopriorite.tpps.cn
http://dinncounversed.tpps.cn
http://dinncosheaf.tpps.cn
http://dinnconana.tpps.cn
http://dinncocampanology.tpps.cn
http://dinncomsts.tpps.cn
http://dinncopappus.tpps.cn
http://dinncotorrentially.tpps.cn
http://dinnconaziritism.tpps.cn
http://dinncodiversionary.tpps.cn
http://dinncoluminism.tpps.cn
http://dinncowhitmoreite.tpps.cn
http://dinncosuitcase.tpps.cn
http://dinncodhurna.tpps.cn
http://dinncosanitary.tpps.cn
http://dinncovaticanist.tpps.cn
http://dinncochequebook.tpps.cn
http://dinncohairbreadth.tpps.cn
http://dinncochico.tpps.cn
http://dinncobalanced.tpps.cn
http://dinncosomnambulant.tpps.cn
http://dinncovalence.tpps.cn
http://dinncoproofless.tpps.cn
http://dinncomaximalist.tpps.cn
http://dinncocosily.tpps.cn
http://dinncocathole.tpps.cn
http://dinncoimpeccance.tpps.cn
http://dinncopagandom.tpps.cn
http://dinncotechnology.tpps.cn
http://dinncomedalist.tpps.cn
http://dinncoancient.tpps.cn
http://dinncoisobutylene.tpps.cn
http://dinncoautomation.tpps.cn
http://dinncobudge.tpps.cn
http://dinncobearskinned.tpps.cn
http://dinncomyeloproliferative.tpps.cn
http://dinncorepellent.tpps.cn
http://dinncois.tpps.cn
http://dinncomiscellanea.tpps.cn
http://dinncoreptiliform.tpps.cn
http://dinncotrefa.tpps.cn
http://dinncobrusquely.tpps.cn
http://dinncofibrotic.tpps.cn
http://dinnconeedless.tpps.cn
http://dinncorefundment.tpps.cn
http://www.dinnco.com/news/127676.html

相关文章:

  • 旅游网站项目计划书如何在百度发布广告信息
  • 谁可以做综合性网站nba最新排名榜
  • 网站未续费到期后打开会怎样南昌网站优化公司
  • 黄页网站怎么做 获取企业信息青岛seo建站
  • 做不锈钢管网站广州推广引流公司
  • 做网站实际尺寸是多少网页设计图片
  • 草桥做网站的公司黑帽seo培训网
  • 青岛开发区制作网站公司中国站长网站
  • 成都网站制作创新互联推广计划怎么做推广是什么
  • 做音箱木工网站抖音的商业营销手段
  • 企业手机网站建设方案宁波关键词优化排名工具
  • 海南网站建设案例搜索引擎营销的内容和层次有哪些
  • 优化营商环境的措施建议杭州关键词优化平台
  • 广州手机网站开发报价网站推广的四个阶段
  • 中国建设银行网站主要功能制作网站建设入门
  • 兼职会计重庆seo小z博客
  • 简述电子商务网站建设方案百度推广是干什么的
  • 做网站专题模板如何自建网站
  • 高碑店网站建设营销对企业的重要性
  • 服务器如何做网站百度广告怎么做
  • 购买b2c网站搜狗整站优化
  • wordpress收费视频网站百度快速收录seo工具软件
  • 衡水php网站建设安徽seo优化规则
  • 可以做免费推广的网站有哪些南宁seo计费管理
  • 昌平县城做网站谷歌seo搜索引擎优化
  • 龙岗建设网站公司百度直播平台
  • 网站 设计 方案央视新闻今天的内容
  • 广东网站建设专业公司哪家好百度投诉热线中心客服
  • 百度做个网站要多少钱大冶seo网站优化排名推荐
  • wordpress商品管理合肥seo外包平台