栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 11 线程入门

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 11 线程入门

C++ 多线程

C++ 开发过程中多线程是基本的技能,很多Coder虽然在使用多线程但可能用C++11里的std::thread机会还真不多,比如

windows开发微软的Createthread;Qt工程师常用的Qthread;linux平台开发常用的pthread;

相比较这些而言std::thread的优势,真的不明显····Createthread和Qthread太有局限性,分别依赖windows平台和Qt库,而pthread简单粗暴,还不支持windows,无法跨平台开发,总之用好std::thread对自己来说是个巨大的提升。

std::thread入门代码
#include
#include 
using namespace std;
class ClassFunc {
public:
	void hello() {
		std::cout << "ClassFunc Hello  World!  "
			  <<"thread id="<< std::this_thread::get_id()  << std::endl; 
	}
};
void NoParamDone() 
{
	cout << "thread id=" << std::this_thread::get_id() << endl;
	cout << "World!" << endl; 
}
void HaveParamDone(int a,int b)
{
	cout << "thread id=" << std::this_thread::get_id() << endl;
	cout << "World!"<< " " << a << " " << b << endl;
}
int main()
{
	cout << "main thread id=" << std::this_thread::get_id() << endl;
	thread thread_lambda([] { 
		cout << "thread id="< 

用起来难么?—很简单

std::thread,支持有参、无参、lambda表达式、类的普通成员函数;个人认为写起来方便优雅

join和detach 两种启动线程的方式

join 在一个线程环境下开启一个子线程并调用了join(),当前线程将会一直阻塞直到被启动的那个子线程返回为止;detach 在一个线程环境下开启一个子线程并调用了detach(),当前线程不会等待子线程结束才结束;子线程转为后台线程,子线程将由系统(运行时库)托管,子线程的“死活”就和主线程无关。子线程结束后,由系统自动释放其资源。也称为守护线程;

#include
#include 
using namespace std;
void TestFunc()
{
	std::chrono::milliseconds dura(25);
	for (int i = 0; i < 5; i++) {
		cout << "thread id=" << std::this_thread::get_id() << "index=" << i << endl;
		std::this_thread::sleep_for(dura);
	}
}

int main()
{
	cout << "main thread id=" << std::this_thread::get_id() << endl;
	thread thread_nor1(TestFunc);
	thread_nor1.join();
	//thread_nor1.detach();
	std::chrono::milliseconds dura(25);
	for (int i = 0; i < 5; i++) {
		cout << "main thread id=" << std::this_thread::get_id() << "index=" << i << endl;
	}
	system("pause");
}

Join的运行结果

detach的运行结果

不调用join或detach 会发生什么?

先说结论:线程依然被启动执行,join或detach只改变父子线程的结束顺序关系和子线程的状态

C++ 11 线程状态

C++11中,线程创建后,有两种状态:

joinablenonjoinable

对应以下两种情况:

默认构造函数构造后状态为nonjoinable;线程对象通过有参构造函数创建后状态为joinable。调用join()/detach()会变成nonjoinable状态;

当thread对象析构时,会判断joinable状态,如果当前对象是joinable的,就会调用std::terminate(),结束进程。所以既不调用detach又不调用join,可能会导致crash 。(这个绝对是个槽点,C++20中jthread对这个槽点进行了修改··)
thread 析构源码

~thread()
{
    if (joinable())
    std::terminate();
}

不调用join或detach 导致crash代码

#include
#include 
using namespace std;
void NoParamDone()
{
	std::chrono::milliseconds dura(25);
}
int main()
{
	cout << "main thread id=" << std::this_thread::get_id() << endl;
	thread thread_t;
	cout << "thread_t joinable=" << thread_t.joinable() << endl;
	thread_t = std::thread(NoParamDone);
	//system("pause");
	std::this_thread::sleep_for(chrono::seconds(10));
}

C++ 11 封装的sleep

C++ 11之前没有通用的休眠函数。c语言的sleep、usleep其实都是系统提供的函数,不同的系统函数的功能还有些差异。
在Windows系统中,sleep的参数是毫秒。
在Unix系统中,sleep函数的单位是秒
C++ 提供了跨平台的同一休眠函this_thread::sleep_for

this_thread::sleep_for(chrono::seconds(1));//sleep 1秒
this_thread::sleep_for(chrono::hours(1));//sleep 1小时
this_thread::sleep_for(chrono::minutes(1));//sleep 1分钟
this_thread::sleep_for(chrono::milliseconds(1));//sleep 1毫秒
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737916.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号