![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
多线程
哞哞哞咩咩咩
记录日常学习
展开
-
12. 补充知识,线程池浅谈,数量谈,总结
补充一些知识点虚假唤醒wait中有第二参数(lambda)并且这个lambda中要正确判断要处理的公共数据是否存在C++多线程编程条件变量虚假唤醒(spurious wakeup)atomic#include <iostream>#include <thread>#include<vector>#include<string>#include<queue>#include<mutex>#include<ato原创 2020-09-25 11:55:20 · 79 阅读 · 0 评论 -
11.windows临界区,其他各种mutex互斥量
1.windows临界区和c++11中的mutex非常类似#include <iostream>#include <thread>#include<vector>#include<string>#include<queue>#include<mutex>#include<windows.h>using namespace std;#define __WINDOWSJQ__ //一个开关,用window原创 2020-09-22 12:44:43 · 200 阅读 · 0 评论 -
10.std::atomic续谈,std::async深入谈
1.原子操作std::atomic续谈一般原子操作争对++,–,+=,&=,|=,^=是支持的,其他的可能不支持#include <iostream>#include <vector>#include <ctime>#include<thread>#include<mutex>#include<future>#include<atomic>using namespace std;std::at原创 2020-09-22 11:26:30 · 100 阅读 · 0 评论 -
9.future其他成员函数,shared_future,atomic
1.future其他成员函数std::future_status::timeout等待1秒,因为线程入口函数要5秒,所以超时了(没有等到)#include <iostream>#include <vector>#include <ctime>#include<thread>#include<mutex>#include<future>using namespace std;int mythread() {//线程入原创 2020-09-21 17:15:24 · 158 阅读 · 0 评论 -
8.async,future, packaged_task, promise
一.std::async, std::future创建后台任务并返回值普通函数形式#include <iostream>#include <vector>#include <ctime>#include<thread>#include<mutex>#include<future>using namespace std;int mythread() {//线程入口函数 cout << "mythread(原创 2020-09-17 21:20:28 · 70 阅读 · 0 评论 -
7.condition_variable,wait,notify_one,notify_all
condition_variable, wait, notify_one#include <iostream>#include <thread>#include<vector>#include<string>#include<queue>#include<mutex>using namespace std;class A {public: void inMsg() { for (int i = 0; i <原创 2020-08-31 20:33:34 · 196 阅读 · 0 评论 -
6.单例设计模式共享数据分析,call_once
单例设计模式#include<iostream>#include<thread>#include<vector>#include<string>#include<queue>#include<mutex>using namespace std;class MyCAS {//单例类private: MyCAS() {}//私有化构造函数 static MyCAS * m_instance;//静态成员变量pub原创 2020-08-31 16:23:11 · 113 阅读 · 0 评论 -
5.unique_lock
unique_lock取代lock_guard#include <iostream>#include <thread>#include<vector>#include<string>#include<queue>#include<mutex>using namespace std;class A {public: void inMsg() { for (int i = 0; i < 10000; i++)原创 2020-08-27 15:54:14 · 100 阅读 · 0 评论 -
4.lock,unlock,lock_guard,死锁
lock,unlock成对出现#include <iostream>#include <thread>#include<vector>#include<string>#include<queue>#include<mutex>using namespace std;class A {public: void inMsg() { for (int i = 0; i < 10000; i++) { co原创 2020-08-18 15:20:56 · 764 阅读 · 0 评论 -
3.创建多个线程,共享数据要加锁
创建多个线程#include <iostream>#include <thread>#include<vector>#include<string>using namespace std;void myprint(int num) { cout << "子线程开始执行,编号:" << num << endl; //......... cout << "子线程结束执行,编号:" <<原创 2020-08-17 11:55:44 · 298 阅读 · 0 评论 -
2.使用datach的坑
不能传指针!#include <iostream>#include <thread>using namespace std;void myprint(const int& i, char * pmybuf) { cout << i << endl;//拷贝的引用,即使datach也是安全的,但是简单类型最好还是直接值传递 cout << pmybuf << endl;//不安全,这指针地址和主函数的mybuf一样原创 2020-08-16 23:10:27 · 705 阅读 · 0 评论 -
1.线程启动,detach,join
join#include <iostream>#include <thread>using namespace std;void myPrint(){ cout << "我的线程开始运行" << endl; //------------- //------------- cout << "我的线程运行完毕" << endl; return;}int main(){ //(1)创建了线程,线程执行起点(入口原创 2020-08-16 17:43:52 · 119 阅读 · 0 评论