自汇合的多线程

C++11以来提供了C++原生的多线程std::thread,这极大的方便了多线程的书写。在此之前书写多线程时需要平台原生API,这对于跨平台尤其是跨多平台程序来讲,代码书写及维护都是极大的工作量。std::thread具有非常高的优势,但是其也有自己的缺点,以下代码为例,

cpp 复制代码
void using_thread_with_no_join()
{
	std::thread t{[](){
		std::cout<<"sub thread execate, thread id"<<std::this_thread::get_id();
	}};
}

运行如上代码时,会出现崩溃,堆栈信息如下, 由如上堆栈信息可知,崩溃原因为std::thread在析构时,如果对象仍为joinable状态,则会触发中断,为避免崩溃需要在std::thread析构器前需要将其置于非joinable状态,即需要主动调用join或detach接口。如果忘记了便会出现如上的崩溃。 既然已经有了RAII思想了,那必然是可以通过该思想来解决忘记join或detach导致崩溃的问题。所以std::jthread应运而生。当然std::jthread不止于此。

std::jthread

剖析其源码是了解其机理的最好方法,std::jthread的部分源码整理如下:

cpp 复制代码
#if _HAS_CXX20
class jthread {
public:
    jthread() noexcept : _Impl{}, _Ssource{nostopstate} {}

    template <class _Fn, class... _Args, enable_if_t<!is_same_v<remove_cvref_t<_Fn>, jthread>, int> = 0>
    _NODISCARD_CTOR explicit jthread(_Fn&& _Fx, _Args&&... _Ax) {
			//-------
    }

    ~jthread() {
        _Try_cancel_and_join();
    }

    jthread(const jthread&)     = delete;
    jthread(jthread&&) noexcept = default;
    jthread& operator=(const jthread&) = delete;

    jthread& operator=(jthread&& _Other) noexcept {
			//--------
    }

private:
    void _Try_cancel_and_join() noexcept {
        if (_Impl.joinable()) {
            _Ssource.request_stop();
            _Impl.join();
        }
    }

    thread _Impl;
    stop_source _Ssource;
};
#endif // _HAS_CXX20

由以上代码可知:

  1. 关注其构造函数:jthread不存在拷贝构造函数和拷贝赋值,存在移动构造函数和移动赋值运算符,即jthread不可拷贝但是可以转移。
  2. 关注其成员变量_Impl为std::thread类型,即std::jthread确系采用RAII思想,在构造函数内构造std::thread,但是在其析构函数内判断是否为joinable状态,若其为joinable状态则调用std::thread的join函数,致使std::thread在析构时恒为非joinable,不会触发崩溃。关于此部分功能不再赘述,完全为std::thread的套壳。
  3. 关注其成员变量_Ssource为std::stop_source类型,std::stop_source内维护stop_source的状态,其状态为std::_Stop_state,而std::_Stop_state实则是原子变量,通过判断该原子变量的值来处理线程的外部请求中断。

std::jthread使用实例

cpp 复制代码
	std::jthread j{[]{
		std::cout << "sub jthread execuate, thread id" << std::this_thread::get_id();
	}};

	//正常输出,并未崩溃,某次执行结果如下
	//sub jthread execuate, thread id35732

std::jthread处理外部请求中断

std::jthread提供三个接口并配合std::stop_token的stop_requested来实现外部请求中段处理。

cpp 复制代码
//std::jthread
_NODISCARD stop_source get_stop_source() noexcept {
        return _Ssource;
    }

 _NODISCARD stop_token get_stop_token() const noexcept {
     return _Ssource.get_token();
 }

 bool request_stop() noexcept {
     return _Ssource.request_stop();
 }

//stop token
 _NODISCARD bool stop_requested() const noexcept {
     const auto _Local = _State;
     return _Local != nullptr && _Local->_Stop_requested();
 }

处理外部请求中断的代码样例如下

cpp 复制代码
void using_jthread_with_stop_token()
{
	std::jthread j{ [](std::stop_token token) {
		std::cout << "sub jthread execate, thread id" << std::this_thread::get_id()<<"\n";
		for (int i =0; i< 20; i++)
		{
			std::cout<<"sub jthread "<<i<<"\n";
			std::this_thread::sleep_for(std::chrono::seconds(1));
			if (token.stop_requested())
			{
				std::cout<<"exit sub jthread " << std::this_thread::get_id() << "\n";
				return;
			}
		}
	} };

	std::cout << "running main thread "<<std::this_thread::get_id()<<"\n";
	std::this_thread::sleep_for(std::chrono::seconds(5));
	j.request_stop();
	std::cout << "exit main thread " << std::this_thread::get_id() << "\n";
}

//output result:
/*
running main thread 34396
sub jthread execate, thread id21536
sub jthread 0
sub jthread 1
sub jthread 2
sub jthread 3
sub jthread 4
exit main thread 34396
exit sub jthread 21536
*/

由源码可知,除直接使用std::jthread对象请求中断外,还可以使用source,即通过std::jthread的get_stop_source接口获得其source,而后通过source来请求中断,示例代码如下:

cpp 复制代码
void using_jthread_with_source_request_stop()
{
	std::jthread j{ [](std::stop_token token) {
		std::cout << "sub jthread execuate, thread id " << std::this_thread::get_id() << "\n";
		for (int i = 0; i < 20; i++)
		{
			std::cout << "sub jthread " << i << "\n";
			std::this_thread::sleep_for(std::chrono::seconds(1));
			if (token.stop_requested())
			{
				std::cout << "exit sub jthread " << std::this_thread::get_id() << "\n";
				return;
			}
		}
	} };

	
	auto source = j.get_stop_source();
	std::thread  t{[](std::stop_source source){
		std::cout << "running t thread " << std::this_thread::get_id() << "\n";
		std::this_thread::sleep_for(std::chrono::seconds(5));
		source.request_stop();
	
	},source};
	t.join();
	std::cout << "t thread  joined" << "\n";
}
//output result:
/*
running t thread 20280
sub jthread execuate, thread id 4164
sub jthread 0
sub jthread 1
sub jthread 2
sub jthread 3
sub jthread 4
t thread  joined
exit sub jthread 4164

*/

总结

  1. std::jthread析构自动汇合,不回崩溃;
  2. std::jthread支持joinable、join、detach、get_id、hardware_concurrency等原生std::thread的接口,故std::jthread可以无缝替换std::thread;
  3. std::jthread支持外部请求中断,无需再向使用std::thread那样,提供一个标志位来作为线程启停的标志

欢迎关注同名公众号【程序员的园】

相关推荐
前进吧-程序员33 分钟前
现代 C++ 异步编程:从零实现一个高性能 ThreadPool (C++20 深度实践)
开发语言·c++·c++20
pearlthriving1 小时前
c++当中的泛型思想以及c++11部分新特性
java·开发语言·c++
t***5442 小时前
Dev-C++中哪些选项可以设置
开发语言·c++
2301_803554522 小时前
C++ 并发核心:std::promise、std::future、std::async 超详细全解
开发语言·c++
EverestVIP2 小时前
C++ 成员函数的指针
c++
俺不要写代码2 小时前
线程启动、结束,创建线程多法、join,detach,线程的移动语义
服务器·开发语言·网络·c++
思麟呀3 小时前
应用层协议HTTP
linux·服务器·网络·c++·网络协议·http
小徐不徐说3 小时前
面试C++易错点总结
开发语言·c++·面试·职场和发展·程序设计·工作
xier_ran4 小时前
【C++】“内部”、“外部”、“派生类”、“友元“类
java·开发语言·c++
熬夜敲代码的猫4 小时前
C/C++:内存管理
c语言·c++·动态内存管理