c++11并发之std::thread
知识链接:
https://www.cnblogs.com/lidabo/p/7852033.html
构造函数如下:
default ()
thread() noexcept;
initialization()
template <class Fn, class... Args> explicit thread (Fn&& fn, Args&&... args);
copy [deleted] ()
thread (const thread&) = delete;
move []
thread (thread&& x) noexcept;
().默认构造函数,创建一个空的 thread 执行对象。 ().初始化构造函数,创建一个 thread 对象,该 thread 对象可被 joinable,新产生的线程会调用 fn 函数,该函数的参数由 args 给出。 ().拷贝构造函数(被禁用),意味着 thread 不可被拷贝构造。 ().move 构造函数,move 构造函数,调用成功之后 x 不代表任何 thread 执行对象。 注意:可被 joinable 的 thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached
#include<thread>
#include<chrono>
#include <iostream>
using namespace std;
void fun1(int n) //初始化构造函数
{
cout << "Thread " << n << " executing\n";
n += ;
this_thread::sleep_for(chrono::milliseconds());
}
void fun2(int & n) //拷贝构造函数
{
cout << "Thread " << n << " executing\n";
n += ;
this_thread::sleep_for(chrono::milliseconds());
}
int main()
{
int n = ;
thread t1; //t1不是一个thread
thread t2(fun1, n + ); //按照值传递
t2.join();
cout << "n=" << n << '\n';
n = ;
thread t3(fun2, ref(n)); //引用
thread t4(move(t3)); //t4执行t3,t3不是thread
t4.join();
cout << "n=" << n << '\n';
system("pause");
return ;
}
#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
using namespace std; void running()
{
cout << "thread is running..." << endl;
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 栈上
thread t1(running); // 根据函数初始化执行
thread t2(running);
thread t3(running); // 线程数组
thread th[] {thread(running), thread(running), thread(running)}; // 执行 // 堆上
thread* pt1(new thread(running));
thread* pt2(new thread(running));
thread* pt3(new thread(running)); // 线程指针数组
thread* pth(new thread[]{thread(running), thread(running), thread(running)}); return a.exec();
}
多线程传递参数
#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
using namespace std; void running(const char* str,const int id)
{
cout << "thread" << id << "is running..."<< str << endl;
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 栈上
thread t1(running,"hello1",); // 根据函数初始化执行
thread t2(running,"hello2",);
thread t3(running,"hello3",); return a.exec();
}
join
join 是让当前主线程等待所有的子线程执行完,才能退出。
#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
using namespace std; void running(const char* str,const int id)
{
cout << "thread" << id << "is running..."<< str << endl;
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 栈上
thread t1(running,"hello1",); // 根据函数初始化执行
thread t2(running,"hello2",);
thread t3(running,"hello3",); cout << t1.joinable() << endl;
cout << t2.joinable() << endl;
cout << t3.joinable() << endl; t1.join(); // 主线程等待当前线程执行完成再退出
t2.join();
t3.join(); return a.exec();
}
detach
#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
using namespace std; void running(const char* str,const int id)
{
cout << "thread" << id << "is running..."<< str << endl;
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 栈上
thread t1(running,"hello1",); // 根据函数初始化执行
thread t2(running,"hello2",);
thread t3(running,"hello3",); cout << t1.joinable() << endl;
cout << t2.joinable() << endl;
cout << t3.joinable() << endl; t1.detach();
t2.detach();
t3.detach(); return a.exec();
}
获取cpu核心个数
#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
using namespace std; int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
auto n = thread::hardware_concurrency();//获取cpu核心个数
cout << n << endl; # return a.exec();
}
CPP原子变量与线程安全。
#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
using namespace std; const int N = ;
int num = ; void run()
{
for (int i = ; i < N; ++i){
num++;
}
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); clock_t start = clock(); thread t1(run);
thread t2(run);
t1.join();
t2.join(); clock_t end = clock();
cout << "num=" << num << ",spend time:" << end - start << "ms" << endl; return a.exec();
}
运行结果:num=1157261,spend time:9ms
结果并不是200000,这是由于线程之间的冲突
互斥量
#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
#include <mutex>
using namespace std; const int N = ;
int num = ;
mutex m;
void run()
{
m.lock();
for (int i = ; i < N; ++i){
num++;
}
m.unlock();
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); clock_t start = clock(); thread t1(run);
thread t2(run);
t1.join();
t2.join(); clock_t end = clock();
cout << "num=" << num << ",spend time:" << end - start << "ms" << endl; return a.exec();
}
运行结果:num=2,spend time:5ms
原子变量。
#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
#include <mutex>
using namespace std; const int N = ;
atomic_int num {}; // 不会发生线程冲突,线程安全 void run()
{
for (int i = ; i < N; ++i){
num++;
}
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); clock_t start = clock(); thread t1(run);
thread t2(run);
t1.join();
t2.join(); clock_t end = clock();
cout << "num=" << num << ",spend time:" << end - start << "ms" << endl; return a.exec();
}
C++11 并发之std::atomic。
lambda与多线程
#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
#include <mutex>
using namespace std; int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); auto fun = [](const char* str){cout << str << endl;};
thread t1(fun,"hello world");
thread t2(fun,"hello C++"); return a.exec();
}
时间等待相关
#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
#include <mutex>
using namespace std; int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); auto fun = [](const char* str){
this_thread::sleep_for(chrono::seconds());
this_thread::yield();// 让cpu执行其他空闲线程
cout << this_thread::get_id() << endl;
cout << str << endl;
};
thread t1(fun,"hello world"); return a.exec();
}
c++11并发之std::thread的更多相关文章
- C++11并发之std::thread<转>
最近技术上没什么大的收获,也是悲催的路过~ 搞一点新东西压压惊吧! C++11并发之std::thread 知识链接: C++11 并发之std::mutex C++11 并发之std::atomic ...
- C++11 并发之std::thread std::mutex
https://www.cnblogs.com/whlook/p/6573659.html (https://www.cnblogs.com/lidabo/p/7852033.html) C++:线程 ...
- C++11并发之std::mutex
知识链接: C++11并发之std::thread 本文概要: 1. 头文件. 2.std::mutex. 3.std::recursive_mutex. 4.std::time_mutex. 5 ...
- C++11 并发指南------std::thread 详解
参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...
- C++11并发——多线程std::thread (一)
https://www.cnblogs.com/haippy/p/3284540.html 与 C++11 多线程相关的头文件 C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是< ...
- c++11中关于std::thread的join的思考
c++中关于std::thread的join的思考 std::thread是c++11新引入的线程标准库,通过其可以方便的编写与平台无关的多线程程序,虽然对比针对平台来定制化多线程库会使性能达到最大, ...
- c++11中关于`std::thread`线程传参的思考
关于std::thread线程传参的思考 最重要要记住的一点是:参数要拷贝到线程独立内存中,不管是普通类型.还是引用类型. 对于传递参数是引用类型,需要注意: 1.当指向动态变量的指针(char *) ...
- C++ 11 笔记 (五) : std::thread
这真是一个巨大的话题.我猜记录完善绝B需要一本书的容量. 所以..我只是略有了解,等以后用的深入了再慢慢补充吧. C++写多线程真是一个痛苦的事情,当初用过C语言的CreateThread,见过boo ...
- Cocos2dx 3.0 过渡篇(二十六)C++11多线程std::thread的简单使用(上)
昨天练车时有一MM与我交替着练,聊了几句话就多了起来,我对她说:"看到前面那俩教练没?老色鬼两枚!整天调戏女学员."她说:"还好啦,这毕竟是他们的乐趣所在,你不认为教练每 ...
随机推荐
- 分布式监控系统Zabbix--完整安装记录 -添加apache监控
前面介绍了zabbix3.0.3环境及相关监控项的添加,下面介绍下针对apache的监控配置:1)在apache配置文件中打开server-status状态访问功能(自带的) [root@IDC-Ad ...
- Peer Programming Project: 4 Elevators Scheduler 学号后三位 157,165
1.Advantages and disanvantages of Peer Programming advantages The code are constantly validated by t ...
- jdk自带的jvisualvm-监控远程linux
简介 jdk有好多自带的工具比如jconsole.jvisualvm.jstatd等 Windows的路径:%JAVA_HOME/bin/目录下,配置好环境变量直接用cmd执行jvisualvm命令即 ...
- C程序设计教学小结(选择结构)
1. 函数使用的三个问题 函数声明语句 void add(); 或 int add(int x,int y); 函数调用 add(); c=add(a,b) 函 ...
- 剑指offer:字符串的排列
题目描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述: ...
- 毕业设计心得与整理-APP-主题切换
1.定义主体颜色: 在style自定义了三个属性: <item name="textLight">@android:color/white</item> & ...
- 项目引入android-support-v7-appcompat遇到的问题,no resource found that matches the given name 'android:Theme.AppCompat.Light'
一.问题 今天准备使用v7包中的ToolBar来用,但是在styles.xml中引入Theme.AppCompat.Light的时候,报错“no resource found that matches ...
- Java中对域和静态方法的访问不具有多态性
1.将方法调用同方法主体关联起来被称为 2.编译期绑定(静态)是在程序编译阶段就确定了引用对象的类型 3.运行期绑定(动态绑定)是指在执行期间判断所引用对象的实际类型,根据其实际的类型调用其相应的方法 ...
- Baltic Dry Index
波罗的海干散货指数(Baltic Dry Index,缩写BDI),是航运业的经济指标,它包含了航运业的干散货交易量的转变. BDI波罗的海指数是由几条主要航线的即期运费(Spot Rate)加权计算 ...
- BZOJ1014 JSOI2008火星人(splay+哈希)
splay维护哈希值即可. #include<iostream> #include<cstdio> #include<cmath> #include<cstd ...