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<iostream>
#include <thread>
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::this_thread::get_id()<<endl;
		cout << "Hello" <<endl; 
	});
	thread thread_nor1(NoParamDone);
	thread thread_nor2(HaveParamDone, 66, 77);

	{
		ClassFunc   class_func;
		std::thread t_class(&ClassFunc::hello, &class_func);
		t_class.join();
	}
	thread_lambda.join();
	thread_nor1.join();
	thread_nor2.join();

	system("pause");
}
/*
输出:
main thread id=15248
thread id=12616
Hello
thread id=1240
World! thread id=13412
World!
66 ClassFunc Hello  World!  thread id=12956
77
*/

用起来难么?—很简单

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

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

  • join 在一个线程环境下开启一个子线程并调用了join(),当前线程将会一直阻塞直到被启动的那个子线程返回为止;
  • detach 在一个线程环境下开启一个子线程并调用了detach(),当前线程不会等待子线程结束才结束;子线程转为后台线程,子线程将由系统(运行时库)托管,子线程的“死活”就和主线程无关。子线程结束后,由系统自动释放其资源。也称为守护线程;
#include<iostream>
#include <thread>
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中,线程创建后,有两种状态:

  • joinable
  • nonjoinable

对应以下两种情况:

  • 默认构造函数构造后状态为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<iostream>
#include <thread>
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毫秒
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值