C++多线程编程:深入剖析std::thread的使用方法
一、线程std::thread简介std::thread是 C11 中引入的一个库用于实现多线程编程。它允许程序创建和管理线程从而实现并发执行。std::thread在#includethread头文件中声明因此使用std::thread时需要包含#includethread头文件。二、语法2.1、构造函数1默认构造函数创建一个空的 thread 执行对象。代码语言C自动换行AI代码解释thread() _NOEXCEPT { // construct with no thread _Thr_set_null(_Thr); }2初始化构造函数创建std::thread执行对象该thread对象可被joinable新产生的线程会调用threadFun函数该函 数的参数由args给出。代码语言C自动换行AI代码解释templateclass Fn,class ... Args explicit thread(Fn fn,Args ... args);表示既可以传入左值也可以传入右值。3拷贝构造函数。代码语言C自动换行AI代码解释// 如果拷贝构造函数被禁用意味着 thread 不可被拷贝构造。 thread(const thread) delete;4move构造函数 。代码语言C自动换行AI代码解释thread(thread x)noexceptmove构造函数调用成功之后x不代表任何thread执行对象。注意可被joinable的thread对象必须在他们销毁之前被主线程join或者将其设置为detached。示例展开代码语言C自动换行AI代码解释#include iostream #include thread using namespace std; void thread_func(int a) { cout thread_func: a (a 10) endl; } int main() { int x 10; thread t1(thread_func, ref(x)); thread t2(move(t1)); // t1 线程失去所有权 thread t3; t3 move(t2); // t2 线程失去所有权 // t1.join(); //执行会报错已放弃 (核心已转储) t3.join(); cout main end: x x endl; return 0; }执行结果代码语言Bash自动换行AI代码解释thread_func: a 20 main end: x 202.2、主要成员函数1get_id()获取线程ID返回类型std::thread::id对象。2joinable()判断线程是否可以加入等待。3join()等该线程执行完成后才返回。4detach()detach调用之后目标线程就成为了守护线程驻留后台运行与之关联的std::thread对象失去对目标线程的关联无法再通过std::thread对象取得该线程的控制权。当线程主函数执行完之后线程就结束了运行时库负责清理与该线程相关的资源。调用 detach 函数之后*this不再代表任何的线程执行实例。joinable() falseget_id() std::thread::id()三、简单线程的创建使用std::thread创建线程提供线程函数或者函数对象并可以同时指定线程函数的参数。传入0个值传入2个值传入引用传入类函数detachmove1传入0个值展开代码语言C自动换行AI代码解释#include iostream #include thread using namespace std; void thread_func1() { cout thread_func1() endl; } int main() { thread t1(thread_func1); // 只传递函数 t1.join(); // 阻塞等待线程函数执行结束 return 0; }2传入2个值展开代码语言C自动换行AI代码解释#include iostream #include thread using namespace std; void thread_func2(int a, int b) { cout thread_func2(): a b a b endl; } int main() { int a 10; int b 16; thread t2(thread_func2, a, b); t2.join(); return 0; }3传入引用展开代码语言C自动换行AI代码解释#include iostream #include thread using namespace std; void thread_func3(int c) { cout thread_func3(): c c cout -- c 10 (c 10) endl; } int main() { int c 10; thread t3(thread_func3, ref(c)); t3.join(); cout main -- 3 : c c , c c endl; return 0; }4传入类函数展开代码语言C自动换行AI代码解释#include iostream #include thread using namespace std; class A { public: void func4(int a) { cout thread: name_ , fun4 a a endl; } void setName(string name) { name_ name; } void displayName() { cout this: this , name: name_ endl; } void play() { std::cout play call! std::endl; } private: string name_; }; int main() { cout test-------------------------- endl; A *a_ptr new A(); a_ptr-setName(hello,C11); thread t4(A::func4, a_ptr, 10); t4.join(); delete a_ptr; A *a_ptr2 new A(); a_ptr2-setName(hello,C14); thread t42(A::func4, a_ptr2, 10);// 传入类的函数地址、类地址、参数 t42.join(); delete a_ptr; return 0; }最好使用取地址符的方式传入类函数避免兼容问题。5detach() 将子线程从主线程中分离出来主线程不再具有管理此子线程的能力。展开代码语言C自动换行AI代码解释#include iostream #include thread using namespace std; void thread_func5() { cout func5 into sleep endl; this_thread::sleep_for(chrono::seconds(2)); cout func5 leave endl; } int main() { thread t5(thread_func5); t5.detach(); // t5.join() // 抛出异常 cout t5 id : t5.get_id() endl; // 抛出异常 cout t5 joinable: t5.joinable() endl; return 0; }执行结果代码语言Bash自动换行AI代码解释t5 id : thread::id of a non-executing thread t5 joinable: 06std::move() 线程所有权转移。展开代码语言C自动换行AI代码解释#include iostream #include thread using namespace std; int main() { thread t6(func6); thread t7(move(t6)); //t6.join(); // 抛出异常 cout t6 id : t6.get_id() endl; cout t6 joinable: t6.joinable() endl; cout t7 joinable: t7.joinable() endl; t7.join(); return 0; }执行结果代码语言Bash自动换行AI代码解释t6 id : thread::id of a non-executing thread t6 joinable: 0 t7 joinable: 1 this is func6 !四、线程封装封装线程子类能继承然后子类能实现具体的业务逻辑。创建线程通过new来实现参数列表和使用构造函数创建是一样的。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2611248.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!