原始C++标准仅支持单线程编程。新的C++标准(称为C++11或C++0x)于2011年发布。在C++11中,引入了新的线程库。因此运行本文程序需要C++至少符合C++11标准。

1 创建线程的三种不同方式

在本章中,我们将讨论如何使用std::thread在C++11中创建线程。

在每个C++应用程序中,都有一个默认的主线程,即main()函数。在C++11中,我们可以通过创建std::thread类的对象来创建其他线程。每个std::thread对象都可以与一个线程关联。因此我们需要引入头文件为:

#include <thread>

那么std::thread在构造函数中接受什么?我们可以在std::thread对象上附加一个回调,该回调将在新线程启动时执行。这些回调可以是:

  1. 函数指针
  2. 函数对象
  3. Lambda函数

1.1 创建线程

可以这样创建线程对象:

std::thread thObj(<CALLBACK>);

新线程将在创建新对象后立即启动,并将与启动该线程的线程并行执行传递的回调。而且,任何线程都可以通过在该线程的对象上调用join()函数来等待另一个线程退出。

让我们看一个示例,其中主线程将创建一个单独的线程。创建此新线程后,主线程将在控制台上打印一些数据,然后等待新创建的线程退出。我们使用三种不同的回调机制来实现上述功能。

  1. 使用函数指针创建线程
#include <thread>
#include <iostream> using namespace std; void thread_function()
{
for (int i = 0; i < 10000; i++);
std::cout << "thread function Executing" << std::endl;
} int main()
{
// 创建线程
std::thread threadObj(thread_function);
for (int i = 0; i < 10000; i++);
std::cout << "Display From MainThread" << std::endl;
// 等待线程的结束
threadObj.join();
std::cout << "Exit of Main function" << std::endl;
return 0;
}

输出为:

Display From MainThreadthread function Executing

Exit of Main function
  1. 使用函数对象创建线程
#include <iostream>
#include <thread> class DisplayThread
{
public:
void operator()()
{
for (int i = 0; i < 10000; i++);
std::cout << "Display Thread Executing" << std::endl;
}
}; int main()
{
std::thread threadObj((DisplayThread()));
for (int i = 0; i < 10000; i++);
std::cout << "Display From Main Thread " << std::endl;
std::cout << "Waiting For Thread to complete" << std::endl;
threadObj.join();
std::cout << "Exiting from Main Thread" << std::endl;
return 0;
}

输出为:

Display Thread ExecutingDisplay From Main Thread
Waiting For Thread to complete Exiting from Main Thread
  1. 使用Lambda函数创建线程
#include <iostream>
#include <thread> int main()
{
int x = 9;
std::thread threadObj([] {
for (int i = 0; i < 10000; i++)
std::cout << "Display Thread Executing" << std::endl;
}); for (int i = 0; i < 10000; i++)
std::cout << "Display From Main Thread" << std::endl; threadObj.join();
std::cout << "Exiting from Main Thread" << std::endl;
return 0;
}

输出为:

Display Thread ExecutingDisplay From Main Thread

Exiting from Main Thread

1.2 区分线程

每个std::thread对象都有一个关联的ID,我们可以使用成员函数来获取,给出关联的thread对象的ID。

std::thread::get_id()

要获取当前线程使用的标识符,即

std::this_thread::get_id()

如果std::thread对象没有关联的线程,则get_id()将返回默认构造的std::id对象,即“没有任何线程”。std::id是一个Object,也可以在控制台上进行比较和打印。让我们看一个例子。

#include <iostream>
#include <thread> void thread_function()
{
std::cout << "Inside Thread::ID  = " << std::this_thread::get_id() << std::endl;
}
int main()
{
std::thread threadObj1(thread_function);
std::thread threadObj2(thread_function); if (threadObj1.get_id() != threadObj2.get_id())
std::cout << "Both Threads have different IDs" << std::endl; std::cout << "From Main Thread::ID of Thread 1 = " << threadObj1.get_id() << std::endl;
std::cout << "From Main Thread::ID of Thread 2 = " << threadObj2.get_id() << std::endl; threadObj1.join();
threadObj2.join();
return 0;
}

输出为:

Inside Thread::ID? = 14756
Inside Thread::ID? = 15500
Both Threads have different IDs
From Main Thread::ID of Thread 1 = 14756
From Main Thread::ID of Thread 2 = 15500

1.3 参考

https://thispointer.com//c-11-multithreading-part-1-three-different-ways-to-create-threads/

最新文章

  1. MySQL 调优/优化的 100 个建议
  2. C语言中内存操作函数
  3. IntelliJ IDEA 编译maven项目以及运行测试前编译项目
  4. YII2.0 验证表单
  5. Mybatis if判断的坑
  6. Delphi WebService 需要注意 转
  7. 【数学】XMU 1597 GCD
  8. C#连接Oracle数据库基本类
  9. hust1010 kmp
  10. hdu 1890 splay树
  11. node.js的基础知识
  12. DQL用户、权限管理(mysql8.0)
  13. git 上传代码
  14. IntelliJ IDEA 中创建maven项目
  15. [20171120]理解v$session的state字段(11G).txt
  16. .NET解决[Serializable] Attribute引发的Json序列化k_BackingField
  17. IntelliJ IDEA 中SpringBoot对Run/Debug Configurations配置 SpringBoot热部署
  18. phpmyadmin误删表后如何恢复
  19. 基于SpringSecurity和JWT的用户访问认证和授权
  20. appfog 使用

热门文章

  1. Windows Socket 接口简介
  2. KubeEdge 1.12版本发布,稳定性、安全性、可扩展性均带来大幅提升
  3. 集合元素的遍历操作,使用迭代器Iterator接口
  4. 硬核剖析ThreadLocal源码,面试官看了直呼内行
  5. 11.mixins混合类
  6. 前后端分离项目(十一):实现&quot;删&quot;功能(前后端)
  7. HPL Study 1
  8. Docker容器化技术
  9. python进阶(26)collections标准库
  10. Flask框架:运用Ajax轮询动态绘图