C++中的std::thread怎么使用

其他教程   发布日期:2025年03月24日   浏览次数:123

这篇文章主要讲解了“C++中的std::thread怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C++中的std::thread怎么使用”吧!

std::thread简介

C++11之前,window和linux平台分别有各自的多线程标准,使用C++编写的多线程往往是依赖于特定平台的。

  • Window平台提供用于多线程创建和管理的win32 api;

  • Linux下则有POSIX多线程标准,Threads或Pthreads库提供的API可以在类Unix上运行;

在C++11新标准中,可以简单通过使用thread库,来管理多线程。thread库可以看做对不同平台多线程API的一层包装;因此使用新标准提供的线程库编写的程序是跨平台的。

一、C++11 线程创建

  • 每一个 C++11 程序都包含一个主线程即 main() 函数,在 C++11 中可以通过创建 std::thread 对象来创建新的线程,每个 std::thread 对象都可以与一个线程相关联。

  • 需要引用的头文件:

  1. #include <thread>

二、std::thread 的构造函数中接收什么参数?

  • 可以给 std::thread 对象添加函数,这个回调函数将在这个新线程启动时执行。这些回调可以是:


    • 函数指针;


    • 函数对象;


    • Lambda 函数。

  • 创建 thread 对象:

  1. std::thread thObj(<CALLBACK>);
  • 新线程将在创建新对象后立即启动,并将并行地执行(当参数)传递给线程的回调函数。此外,任何线程都可以通过调用某线程对象上的 join( ) 函数来等待此线程退出。

  • 来看一个例子,主线程将创建另外一个线程,创建这个新线程后,主线程会在控制台上打印一些数据,然后等待新创建的线程退出。

  • 使用函数指针创建线程:

  1. #include <thread>
  2. void thread_function() {
  3. for(int i = 0; i < 10000; i++);
  4. std::cout<<"thread function Executing"<<std::endl;
  5. }
  6. int main() {
  7. std::thread threadObj(thread_function);
  8. for(int i = 0; i < 10000; i++);
  9. std::cout<<"Display From MainThread"<<std::endl;
  10. threadObj.join();
  11. std::cout<<"Exit of Main function"<<std::endl;
  12. return 0;
  13. }
  • 使用函数对象创建线程:

  1. #include <iostream>
  2. #include <thread>
  3. class DisplayThread {
  4. public:
  5. void operator()() {
  6. for(int i = 0; i < 10000; i++)
  7. std::cout<<"Display Thread Executing"<<std::endl;
  8. }
  9. };
  10. int main() {
  11. std::thread threadObj( (DisplayThread()) );
  12. for(int i = 0; i < 10000; i++)
  13. std::cout<<"Display From Main Thread "<<std::endl;
  14. std::cout<<"Waiting For Thread to complete"<<std::endl;
  15. threadObj.join();
  16. std::cout<<"Exiting from Main Thread"<<std::endl;
  17. return 0;
  18. }
  • 使用 Lambda 函数创建线程:

  1. #include <iostream>
  2. #include <thread>
  3. int main() {
  4. int x = 9;
  5. std::thread threadObj([]{
  6. for(int i = 0; i < 10000; i++)
  7. std::cout<<"Display Thread Executing"<<std::endl;
  8. });
  9. for(int i = 0; i < 10000; i++)
  10. std::cout<<"Display From Main Thread"<<std::endl;
  11. threadObj.join();
  12. std::cout<<"Exiting from Main Thread"<<std::endl;
  13. return 0;
  14. }
  • 如何区分线程:


    • 每个 std::thread 对象都有一个 ID,使用下面的函数可以获取:

  1. std::thread::get_id()

获取当前线程的 ID:

  1. std::this_thread::get_id()
  • 如果 std::thread 对象没有和任何对象关联,则 get_id() 函数会返回默认构造的 std::thread::id 对象,即“非线程”。std::thread::id 是一个对象,它也可以在控制台上进行比较和打印:

  1. #include <iostream>
  2. #include <thread>
  3. void thread_function() {
  4. std::cout<<"Inside Thread :: ID = "<<std::this_thread::get_id()<<std::endl;
  5. }
  6. int main() {
  7. std::thread threadObj1(thread_function);
  8. std::thread threadObj2(thread_function);
  9. if(threadObj1.get_id() != threadObj2.get_id())
  10. std::cout<<"Both Threads have different IDs"<<std::endl;
  11. std::cout<<"From Main Thread :: ID of Thread 1 = "<<threadObj1.get_id()<<std::endl;
  12. std::cout<<"From Main Thread :: ID of Thread 2 = "<<threadObj2.get_id()<<std::endl;
  13. threadObj1.join();
  14. threadObj2.join();
  15. return 0;
  16. }

三、std::thread 的搭配用法

① std::promise

  • 为了在不同的线程之间传递数据,C++ 引入了 std::promise 和 std::future 这两种数据结构,在头文件 <future> 中包含。

  • promise 是一个范型的数据结构,你可以定义一个整形的 promise:promise,这意味着线程之间传递的值是整形。promise 的 get_future() 方法返回一个 future 数据结构,从这个 future 数据结构可以获取设置给 promise 的值:

  1. #include <iostream>
  2. #include <future>
  3. #include <thread>
  4. using namespace std;
  5. int main() {
  6. promise<int> a_promise;
  7. auto a_future = a_promise.get_future();
  8. a_promise.set_value(10);
  9. cout << a_future.get() << endl;
  10. cout << "after get()" << endl;
  11. return 0;
  12. }

输出结构是:

10
after get()

  • 实际上,上面的例子并没有使用线程,但是很好得展示了 promise 和 future 之间的关系。更复杂一点的使用场景可能如下:

    • 主线程定义一个 promise,命名为 p;

    • 主线程调用 p.get_future(),并把返回值保存为引用 f;

    • 主线程启动一个子线程,并把 p 作为启动参数传给子线程;

    • 主线程调用 f.get(),但是此时子线程还未将数据放入 promise 内,所以主线程挂起;

    • 子线程执行完,获取到结果,并把结果写入 p;

    • 主线程从 f.get() 的调用中被唤醒,获取到子线程写入 p 的值,继续执行。

② std::packaged_task

C++11 很贴心地提供 packaged_task 类型,可以不用直接使用 std::thread 和 std::promise,直接就能够生成线程,派遣任务:

  1. #include <iostream>
  2. #include <future>
  3. using namespace std;
  4. int f() {
  5. string hi = "hello, world!";
  6. cout << hi << endl;
  7. return hi.size();
  8. }
  9. int main() {
  10. packaged_task<int ()> task(f);
  11. auto result = task.get_future();
  12. task();
  13. cout << result.get() << endl;
  14. return 0;
  15. }

运行结果为:

hello, world!
13

③ std::async

  • std::packaged_task 要求自己启动任务,比如要显示调用 task(),如果连这一步都想省了的话,可以使用 std:async:

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <numeric>
  5. #include <future>
  6. template <typename RandomIt>
  7. int parallel_sum(RandomIt beg, RandomIt end) {
  8. auto len = end - beg;
  9. if (len < 1000)
  10. return std::accumulate(beg, end, 0);
  11. RandomIt mid = beg + len/2;
  12. auto handle = std::async(std::launch::async,
  13. parallel_sum<RandomIt>, mid, end);
  14. int sum = parallel_sum(beg, mid);
  15. return sum + handle.get();
  16. }
  17. int main() {
  18. std::vector<int> v(10000, 1);
  19. std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '
  20. ';
  21. }

运行结果:

The sum is 10000

④ std::this_thread

  • C++11 专门提供了一个命名空间 std::this_thread 来表示当前线程。

  • std::this_thread 提供了几个方法可以对线程做一定的控制:

    • get_id(),获取线程 id;

    • yield(),释放执行权;

    • sleep_for(),使线程沉睡一定时间。

  1. #include <iostream>
  2. #include <future>
  3. #include <thread>
  4. using namespace std;
  5. int f(promise<int> my_promise) {
  6. string hi = "hello, world!";
  7. my_promise.set_value(hi.size());
  8. this_thread::sleep_for(0.1s);
  9. cout << hi << endl;
  10. }
  11. int main() {
  12. promise<int> f_promise;
  13. auto result = f_promise.get_future();
  14. thread f_thread(f, move(f_promise));
  15. cout << result.get() << endl;
  16. f_thread.join();
  17. return 0;
  18. }

以上就是C++中的std::thread怎么使用的详细内容,更多关于C++中的std::thread怎么使用的资料请关注九品源码其它相关文章!