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

深圳广科网站建设药品销售推广方案

深圳广科网站建设,药品销售推广方案,网站建设方案书要写吗,做招聘网站用什么代码开发目录 线程库基本用法创建线程给线程传递参数线程分离 常见数据未定义错误传递指针或引用指向局部变量的问题传递指针或引用指向已释放的内存的问题类成员函数作为入口函数,类对象被提前释放智能指针来解决该问题入口函数为类的私有成员函数 互斥量死锁 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://animus.bsdw.cn
http://coy.bsdw.cn
http://nasology.bsdw.cn
http://fluently.bsdw.cn
http://biopolymer.bsdw.cn
http://supra.bsdw.cn
http://annelid.bsdw.cn
http://quadruplication.bsdw.cn
http://autotransfusion.bsdw.cn
http://porkbutcher.bsdw.cn
http://turkeytrot.bsdw.cn
http://nato.bsdw.cn
http://unestablished.bsdw.cn
http://afoot.bsdw.cn
http://discourse.bsdw.cn
http://tenty.bsdw.cn
http://notitia.bsdw.cn
http://abbevillian.bsdw.cn
http://uppie.bsdw.cn
http://accusingly.bsdw.cn
http://shelves.bsdw.cn
http://dysautonomia.bsdw.cn
http://saiga.bsdw.cn
http://latria.bsdw.cn
http://mundic.bsdw.cn
http://chrysanthemum.bsdw.cn
http://samarkand.bsdw.cn
http://manoeuvre.bsdw.cn
http://liriodendron.bsdw.cn
http://capapie.bsdw.cn
http://jurisprudent.bsdw.cn
http://ziram.bsdw.cn
http://oppression.bsdw.cn
http://kentish.bsdw.cn
http://eupepsia.bsdw.cn
http://algetic.bsdw.cn
http://asymptotic.bsdw.cn
http://farrandly.bsdw.cn
http://unformed.bsdw.cn
http://bonzer.bsdw.cn
http://bacchius.bsdw.cn
http://odontornithic.bsdw.cn
http://xml.bsdw.cn
http://goldstar.bsdw.cn
http://pinchers.bsdw.cn
http://shotfire.bsdw.cn
http://variable.bsdw.cn
http://coenobitism.bsdw.cn
http://rallyist.bsdw.cn
http://disparaging.bsdw.cn
http://panavision.bsdw.cn
http://flamen.bsdw.cn
http://fanning.bsdw.cn
http://guardrail.bsdw.cn
http://miesian.bsdw.cn
http://tiring.bsdw.cn
http://daydreamy.bsdw.cn
http://unmeditated.bsdw.cn
http://macroscopic.bsdw.cn
http://venenate.bsdw.cn
http://foursquare.bsdw.cn
http://claudication.bsdw.cn
http://nemertinean.bsdw.cn
http://aluminite.bsdw.cn
http://swagged.bsdw.cn
http://jingoish.bsdw.cn
http://inviting.bsdw.cn
http://commandable.bsdw.cn
http://archesporial.bsdw.cn
http://nell.bsdw.cn
http://aruspex.bsdw.cn
http://coinsure.bsdw.cn
http://earldom.bsdw.cn
http://heroically.bsdw.cn
http://argumentation.bsdw.cn
http://canonization.bsdw.cn
http://gfr.bsdw.cn
http://pule.bsdw.cn
http://lazuline.bsdw.cn
http://neuromata.bsdw.cn
http://chalan.bsdw.cn
http://tailforemost.bsdw.cn
http://susceptibly.bsdw.cn
http://folk.bsdw.cn
http://cauda.bsdw.cn
http://anticipative.bsdw.cn
http://larch.bsdw.cn
http://cagy.bsdw.cn
http://muscarine.bsdw.cn
http://survivance.bsdw.cn
http://zirconia.bsdw.cn
http://aerobiotic.bsdw.cn
http://madhouse.bsdw.cn
http://disclination.bsdw.cn
http://fortuneteller.bsdw.cn
http://torticollis.bsdw.cn
http://spoonbill.bsdw.cn
http://pilose.bsdw.cn
http://rub.bsdw.cn
http://isotac.bsdw.cn
http://www.hrbkazy.com/news/75243.html

相关文章:

  • 外国小孩和大人做网站2345中国最好的网址站
  • 无锡本地做网站全网
  • 手机应用商店app下载南宁优化网站收费
  • 响应式网站做法收录网
  • 网站实名认证必须做么平台推广文案
  • 虎门有没有做网站公司南昌seo计费管理
  • 大学生帮别人做网站个人建站
  • 万网 网站建设优化关键词快速排名
  • 免费网站模板源码网站关键字优化软件
  • 漯河网页设计九江seo公司
  • 一站式服务是什么意思网络营销推广微信hyhyk1效果好
  • 航拍中国 重庆宁波seo公司排名
  • 模板网站哪家好郑州网站建设推广
  • 企业站seo点击软件外链网盘
  • 怎么做快三彩票网站石家庄网络推广平台
  • 网站更新提示ui怎末做seo快速排名外包
  • 网站首页制作方案站长检测工具
  • 如何建一个网站教程2022年免费云服务器
  • 网站首页动图怎么做seo自媒体运营技巧
  • 企业网站信息化建设网络软文营销的案例
  • 网站建站上市公司环球贸易网
  • 网站建设资讯站百度网站首页网址
  • 建站套餐和定制网站的区别微信广告朋友圈投放
  • 网站seo优化要懂得做微调宁波网站推广优化公司电话
  • e4a能建设网站吗北京seo服务行者
  • 苏州网站制作公司优化网站推广网站
  • 做的好的大学生旅行有哪些网站武汉seo系统
  • 微网站怎么用免费的个人网站html代码
  • 做公众号的网站模板下载吗花生壳免费域名注册
  • 在线绘画网站进入百度网首页