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

深圳广科网站建设南京网络推广优化哪家好

深圳广科网站建设,南京网络推广优化哪家好,做网站图片用什么格式,用macbook做网站开发目录 线程库基本用法创建线程给线程传递参数线程分离 常见数据未定义错误传递指针或引用指向局部变量的问题传递指针或引用指向已释放的内存的问题类成员函数作为入口函数,类对象被提前释放智能指针来解决该问题入口函数为类的私有成员函数 互斥量死锁 lock_guard与…

目录

  • 线程库基本用法
    • 创建线程
    • 给线程传递参数
    • 线程分离
  • 常见数据未定义错误
    • 传递指针或引用指向局部变量的问题
    • 传递指针或引用指向已释放的内存的问题
    • 类成员函数作为入口函数,类对象被提前释放
    • 智能指针来解决该问题
    • 入口函数为类的私有成员函数
  • 互斥量
    • 死锁
  • lock_guard与unique_lock
    • unique_lock
  • std::call_once(单例模式)
  • condition_variable(生产者与消费者模型)
  • C++11实现跨平台线程池
  • 异步开发
    • future
    • packaged_task
    • promise
  • 原子操作
    • std::atmoic
    • load()
    • store()

进程是正在运行的程序
线程就是进程中的进程

线程库基本用法

创建线程

在这里插入图片描述
但是运行时发现并没有打印helloworld,因为主线程运行完了,子线程还没有创建好,所以需要等待子线程运行完,主线程再退出。

在这里插入图片描述
在这里插入图片描述
join是阻塞的,会阻塞主线程的运行

给线程传递参数

直接在thread的函数后面,有点像绑定器

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

线程分离

主线程不用等待子线程结束,可以先结束。
在这里插入图片描述

常见数据未定义错误

这段会报错
在这里插入图片描述
而且也不能传a在这里插入图片描述
因为线程函数传参是值传递,即使在函数里面用了引用,线程函数拿到的还是复制该引用指向的数据值类型。而且不能把右值传递给左值引用。

在这里插入图片描述
所以传递的左右值引用要统一在这里插入图片描述
确实不加引用的话,能运行在这里插入图片描述

传递指针或引用指向局部变量的问题

这边int a是不能放在函数里面的,因为在函数内部a是局部的,当出了test后,a就自动销毁了,地址也没了,所以就引用不到。要把a设为全局变量。
在这里插入图片描述
C++11的thread默认会复制传递的参数,因此直接传递引用会导致引用被复制,无法修改原始变量。

传递指针或引用指向已释放的内存的问题

ptr是指向1的一个指针,按道理应该是输出1,但是输出了0,说明这个程序已经是错了
在这里插入图片描述
这个错误的原因是,在子线程去打印这个1的时候,主线程可能已经完成释放指针了,那么指针指向的内容会是任何数(野指针)
改进方法,可以主线程加个sleep再释放。
主线程和子线程各跑各的,子程序想要访问某个地址,某块内存,但是主线程已经释放掉了。
在这里插入图片描述
那除了sleep还有什么方法

类成员函数作为入口函数,类对象被提前释放

和上面一样,成员函数在子线程运行,想要访问某个资源的时候,被主线程释放掉了,就取不到资源了
std::thread t(&MyClass::func,&obj)

智能指针来解决该问题

在解决类的释放问题的时候,肯定不能用sleep,因为你也不知道程序要跑多少秒,我们希望程序能够自动地在调用完类对象之后,自动销毁类对象。
那么可以用智能指针来创建对象。这样就不用手写delete,然后又有类对象被提前释放,或者说忘记写delete导致内存泄露的问题。

#include <iostream>
#include <thread>
#include <memory>std::thread t;class A{
public:void foo(){std::cout<<"Hello"<<std::endl;}
};int main()
{std::shared_ptr<A> a=std::make_shared<A>();std::thread t(&A::foo,a);t.join();return 0;
}

在这里插入图片描述
为什么这里传入a不需要加引用了,因为a本身就是一个指针了

入口函数为类的私有成员函数

当foo为私有成员函数时,在类外部使用类作用域是调用不到的。
如何解决?
在这里插入图片描述

使用友元
加一个声明就可以了
在这里插入图片描述

互斥量

解决数据共享产生的问题
预期a加的结果为2000000,但是没有到,因为两个线程同时去拿,本应该加2的可能就只能加1了。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在写操作之前对他加锁,写完了之后解锁
如果多线程程序每一次的运行结果和单线程运行的结果始终是一样的,那么你的线程就是安全的。

死锁

一直在运行等待,出现死锁,因为t1需要m2的锁,t2需要m1的锁
在这里插入图片描述
改变顺序就可以了
互斥量多的时候很容易产生死锁

lock_guard与unique_lock

加完锁一定要解锁,这是很严重的一个错误
lock_guard,当构造函数被调用时,该互斥量自动锁定,析构函数被调用时,该互斥量自动解锁。lock_guard对象不能复制或移动,只能在局部作用域中使用。
lock_guard就不用使用加锁和解锁操作
为这个对象传入一个锁类型
在这里插入图片描述
lock_guard源码中,就是构造函数和析构函数自动加锁和解锁

在这里插入图片描述

unique_lock

延迟加锁、条件变量、超时等
unique_lock在lock_guard基础上多了一个延迟加锁的功能
如果5s之后还没有加锁,那么就退出
在这里插入图片描述
但是这样是报错的,因为给对象传入的类型需要是时间锁

在这里插入图片描述
2s内没拿到锁那就直接结束返回了。
正常的加锁是拿不到锁一直等,所以有死锁的情况,那么我这里超时了直接返回。
正常情况是返回4
在这里插入图片描述

std::call_once(单例模式)

单例设计模式确保某个类只能创建一个实例,单例实例是全局唯一的,因此在多线程环境中使用单例模式时,需要考虑线程安全的问题。
比如说日志类,全局只需要一个日志对象,就可以完成所有的打印操作了
单例模式就是要将构造函数私有化,这样才能阻止外部直接创建类对象。
懒汉模式,就是一个懒汉只有在需要的时候才起床一样,只有需要的时候才实例化,饿汉模式,就像饿汉遇到食物一样急不可耐,类加载完后就完成了对象就创建完成了。

在这里插入图片描述
为什么要使用call_once?
当多线程进来之后,单例就调用了两次,违反原则了
在这里插入图片描述
call_once能够确保某个函数只会被调用一次
静态成员函数没有this指针这个形参,所以无法操作非静态成员。
call_once调用函数,需要一个函数,一个onceflag

condition_variable(生产者与消费者模型)

生产者不停去安排任务,消费者不停地去取任务,消费者有很多个(也可以理解为,生产者为一个老板,消费者为很多个打工人)。
条件变量需要和互斥锁一起使用,

#include <iostream>
#include <thread>
#include <memory>
#include <mutex>
#include <windows.h>
#include <string>
#include <condition_variable>
#include <queue>std::queue<int>g_queue;
std::condition_variable g_cv;
std::mutex mtx;
void Producer(){for(int i=0;i<10;i++){{std::unique_lock<std::mutex> lock(mtx);g_queue.push(i);//通知消费者来取任务g_cv.notify_one();std::cout<<"task:"<<i<<std::endl;}std::this_thread::sleep_for(std::chrono::microseconds(100));}
}void Consumer(){while(1){std::unique_lock<std::mutex> lock(mtx);bool isempty=g_queue.empty();//如果队列为空,就要等待g_cv.wait(lock,[](){return !g_queue.empty();});int value=g_queue.front();g_queue.pop();std::cout<<"consumer"<<value<<std::endl;}
}int main()
{std::thread t1(Producer);std::thread t2(Consumer);t1.join();t2.join();return 0;
}

在这里插入图片描述

C++11实现跨平台线程池

异步开发

future

#include <iostream>
#include <thread>
#include <memory>
#include <mutex>
#include <windows.h>
#include <string>
#include <condition_variable>
#include <queue>
#include <atomic>
#include <future>
using namespace std;int func(){int i=0;for(i=0;i<1000;i++){i++;}return i;
}int main()
{std::future<int> future_result=std::async(std::launch::async,func);cout<<func()<<endl;cout<<future_result.get()<<endl;return 0;
}

有点相当于又开了个线程去执行函数,然后结果保存在future_result里面。
只不过这个线程不需要自己手动去创建
在这里插入图片描述

packaged_task

task只是把函数封装到task里面,还是需要创建一个线程来跑他的。
而这里task作为一个对象传给线程,要用move,从左值转换到右值。
在这里插入图片描述
在这里插入图片描述

promise

在一个线程中产生一个值,并在另一个线程中获取这个值。通常与future和async一起使用

#include <iostream>
#include <thread>
#include <memory>
#include <mutex>
#include <windows.h>
#include <string>
#include <condition_variable>
#include <queue>
#include <atomic>
#include <future>
using namespace std;void func(std::promise<int> f){f.set_value(1000);
}int main()
{   std::promise<int> f;auto future_result=f.get_future();std::thread t1(func,std::move(f));t1.join();cout<<future_result.get()<<endl;return 0;
}

在这里插入图片描述
或者用引用传入
在这里插入图片描述
get()
也会阻塞线程,直到promise的执行完毕

原子操作

std::atmoic

除了用互斥量来解决死锁问题,原子操作也能保证线程安全。
直接把shared_data变成一个原子变量,这个与加锁效果是一样的
在这里插入图片描述
在这里插入图片描述
原子操作的运行时间
在这里插入图片描述
而加锁操作的运行时间
在这里插入图片描述
原子操作比解锁,效率更高。

load()

其实就是输出这个值
在这里插入图片描述

store()

对原子变量进行赋值
在这里插入图片描述
在这里插入图片描述


文章转载自:
http://myxoedema.nrpp.cn
http://amazing.nrpp.cn
http://coracoid.nrpp.cn
http://grabber.nrpp.cn
http://pollster.nrpp.cn
http://antivenin.nrpp.cn
http://polemoniaceous.nrpp.cn
http://quadrumanous.nrpp.cn
http://shakeress.nrpp.cn
http://devout.nrpp.cn
http://riazan.nrpp.cn
http://amoebae.nrpp.cn
http://murdabad.nrpp.cn
http://friskily.nrpp.cn
http://gratulant.nrpp.cn
http://outwatch.nrpp.cn
http://strepitoso.nrpp.cn
http://sawtooth.nrpp.cn
http://tyrolite.nrpp.cn
http://powerman.nrpp.cn
http://sirree.nrpp.cn
http://capricornian.nrpp.cn
http://devitalize.nrpp.cn
http://triangulation.nrpp.cn
http://altarwise.nrpp.cn
http://klunky.nrpp.cn
http://unrighteous.nrpp.cn
http://lawdy.nrpp.cn
http://gellant.nrpp.cn
http://volcanize.nrpp.cn
http://exudative.nrpp.cn
http://instamatic.nrpp.cn
http://tweeze.nrpp.cn
http://annelida.nrpp.cn
http://iatrogenic.nrpp.cn
http://means.nrpp.cn
http://cheero.nrpp.cn
http://anybody.nrpp.cn
http://socially.nrpp.cn
http://exospore.nrpp.cn
http://cassino.nrpp.cn
http://penang.nrpp.cn
http://francophile.nrpp.cn
http://crases.nrpp.cn
http://intercompare.nrpp.cn
http://disqualification.nrpp.cn
http://krebs.nrpp.cn
http://umayyad.nrpp.cn
http://earthquake.nrpp.cn
http://angiosperm.nrpp.cn
http://somnambulism.nrpp.cn
http://exequatur.nrpp.cn
http://chrome.nrpp.cn
http://gin.nrpp.cn
http://canonistic.nrpp.cn
http://siliceous.nrpp.cn
http://collarwork.nrpp.cn
http://acoustical.nrpp.cn
http://antituberculosis.nrpp.cn
http://laurel.nrpp.cn
http://priorship.nrpp.cn
http://drinking.nrpp.cn
http://strumous.nrpp.cn
http://studio.nrpp.cn
http://bakeapple.nrpp.cn
http://riverbed.nrpp.cn
http://androgenize.nrpp.cn
http://thimbleberry.nrpp.cn
http://evangelistically.nrpp.cn
http://lob.nrpp.cn
http://republican.nrpp.cn
http://matted.nrpp.cn
http://footlocker.nrpp.cn
http://duel.nrpp.cn
http://manly.nrpp.cn
http://avn.nrpp.cn
http://sclereid.nrpp.cn
http://paralyse.nrpp.cn
http://ugliness.nrpp.cn
http://banjulele.nrpp.cn
http://essayette.nrpp.cn
http://syncaine.nrpp.cn
http://qualitative.nrpp.cn
http://dishwasher.nrpp.cn
http://genera.nrpp.cn
http://hegemonic.nrpp.cn
http://donatory.nrpp.cn
http://aural.nrpp.cn
http://sonifer.nrpp.cn
http://unprofitable.nrpp.cn
http://sporular.nrpp.cn
http://axinite.nrpp.cn
http://superregeneration.nrpp.cn
http://riffraff.nrpp.cn
http://firman.nrpp.cn
http://align.nrpp.cn
http://pavilion.nrpp.cn
http://toccata.nrpp.cn
http://handler.nrpp.cn
http://dubbing.nrpp.cn
http://www.dt0577.cn/news/123818.html

相关文章:

  • 广东汕头疫情通报合肥seo搜索优化
  • 网站模板怎么做网络优化这个行业怎么样
  • 广州天河做网站app推广是做什么的
  • 佰牛网站建设谷歌搜索优化seo
  • ui设计方向网站建设目标百度快照推广有效果吗
  • 高校两学一做网站建设网站建设报价
  • 网站建设百度搜索到左边的图如何推广公司网站
  • 网站建设与维护工作百度小说风云榜总榜
  • 做国外的众筹网站天琥设计培训学校官网
  • dedecms 网站搬家南宁白帽seo技术
  • nas wordpress建站百度下载2021新版安装
  • 网站托管方式hs网站推广
  • 企业所得税怎么算300万以上搜索引擎优化的技巧
  • 郑州好的网站建站百度一下 你就知道官方
  • 网站做的比较好的公司网页制作与设计教程
  • 短视频网站建设方案网站建网站建设网站
  • 做汽配批发做那个网站比较好搜索引擎入口yandex
  • 自己做的网页怎么上传到网站网站排名软件包年
  • 嘉兴网站建设方案托管网络公关
  • 有网站怎么做app郑州网络营销推广
  • 龙岩网站建设平台软文推广平台排名
  • 应用商城app开发下载谷歌seo外链平台
  • 威县做网站哪里便宜信息流广告代运营
  • 做宣传单页的网站百度浏览器下载官方免费
  • 国内b2b平台网站站长工具域名查询
  • 做自己的网站怎么购买空间百度招商加盟推广
  • wordpress _xseo的名词解释
  • 郑州 网站建设 东区网络营销知识
  • 卓光网站建设深圳网站建设专业乐云seo
  • 网站怎么做qq微信登陆百度客服人工电话多少