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与我交替着练,聊了几句话就多了起来,我对她说:"看到前面那俩教练没?老色鬼两枚!整天调戏女学员."她说:"还好啦,这毕竟是他们的乐趣所在,你不认为教练每 ...
随机推荐
- mongo java 踩坑记
为什么会有这么多坑 1. Java会把 id:String = "合法ObjectId" 好心好意的 转为 _id:ObjectId 类型. 2. 为了避免第1点, 我定义了 ...
- inode 软/硬链接
一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB). 操作系统 ...
- HTML5-indexedDB使用常见错误总结
indexedDB使用过程中常常会出现以下错误: Failed to execute ‘createObjectStore’ on ‘IDBDatabase’: The database is not ...
- ChangeSetenceSort(java)
package com.home.test; import java.util.Arrays; public class ChangeSort { public S ...
- C# 中颜色和名称样式对照表
WPF中的画刷也一样适用 System.Windows.Media.Brushes.名称 (如:System.Windows.Media.Brushes.AliceBlue) :first-child ...
- 开源通用爬虫框架YayCrawler-开篇
各位好!从今天起,我将用几个篇幅的文字向大家介绍一下我的一个开源作品--YayCrawler,其在GitHub上的网址是:https://github.com/liushuishang/YayCraw ...
- ExtJS Tab里放Grid高度自适应问题,官方Perfect方案。
出处:http://docs.sencha.com/extjs/4.2.1/extjs-build/examples/layout-browser/layouts/combination.js // ...
- python使用原始套接字 解析原始ip头数据
使用底层套接字解码底层流量,是这次做的重点工作. 首先来捕获第一个包 # coding:utf-8import socket # 监听的主机IP host = "192.168.1.100& ...
- HashMap的实现原理--链表散列
1. HashMap概述 HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒久不变. ...
- xhtml和html的差別
xhtml和html主要區別: 元素必須正確嵌套: 元素必須正確閉合: 必須要有根元素: 元素必須使用小寫. xhtml語法: 屬性名必須小寫: 屬性值必須帶引號: 使用id代替name: lang屬 ...