网站首页 > 文章精选 正文
原始C++标准仅支持单线程编程。新的C++标准(称为C++11或C++0x)于2011年发布。在C++11中,引入了新的线程库。因此运行本文程序需要C++至少符合C++11标准。
文章目录
- 1 创建线程的三种不同方式
- 1.1 创建线程
- 1.2 区分线程
- 1.3 参考
1 创建线程的三种不同方式
在本章中,我们将讨论如何使用std::thread在C++11中创建线程。
在每个C++应用程序中,都有一个默认的主线程,即main()函数。在C++11中,我们可以通过创建std::thread类的对象来创建其他线程。每个std::thread对象都可以与一个线程关联。因此我们需要引入头文件为:
#include <thread>
那么std::thread在构造函数中接受什么?我们可以在std::thread对象上附加一个回调,该回调将在新线程启动时执行。这些回调可以是:
- 函数指针
- 函数对象
- Lambda函数
1.1 创建线程
可以这样创建线程对象:
std::thread thObj(<CALLBACK>);
新线程将在创建新对象后立即启动,并将与启动该线程的线程并行执行传递的回调。而且,任何线程都可以通过在该线程的对象上调用join()函数来等待另一个线程退出。
让我们看一个示例,其中主线程将创建一个单独的线程。创建此新线程后,主线程将在控制台上打印一些数据,然后等待新创建的线程退出。我们使用三种不同的回调机制来实现上述功能。
- 使用函数指针创建线程
#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
- 使用函数对象创建线程
#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
- 使用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::thread::id对象,即“没有任何线程”。std::thread::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/??
- 上一篇: Qt多线程创建
- 下一篇: Java线程池的正确创建方式
猜你喜欢
- 2025-01-02 Socket与TCP协议,利用python打造一个多人聊天室
- 2025-01-02 探讨C语言系统编程中线程的原理以及实现
- 2025-01-02 ffmpeg播放器实现详解 - 创建线程
- 2025-01-02 互联网面试-Java中如何去创建一个线程池?
- 2025-01-02 一篇详解内核监控进程与线程创建
- 2025-01-02 Python多线程,线程与进程的区别,线程模块及线程的两种创建方式
- 2025-01-02 正确使用线程池的姿势,你在工作中不要只会使用默认的方式创建
- 2025-01-02 Java线程池的正确创建方式
- 2025-01-02 Qt多线程创建
- 2025-01-02 腾讯二面:Linux操作系统里一个进程最多可以创建多少个线程?
- 最近发表
- 标签列表
-
- newcoder (56)
- 字符串的长度是指 (45)
- drawcontours()参数说明 (60)
- unsignedshortint (59)
- postman并发请求 (47)
- python列表删除 (50)
- 左程云什么水平 (56)
- 计算机网络的拓扑结构是指() (45)
- 稳压管的稳压区是工作在什么区 (45)
- 编程题 (64)
- postgresql默认端口 (66)
- 数据库的概念模型独立于 (48)
- 产生系统死锁的原因可能是由于 (51)
- 数据库中只存放视图的 (62)
- 在vi中退出不保存的命令是 (53)
- 哪个命令可以将普通用户转换成超级用户 (49)
- noscript标签的作用 (48)
- 联合利华网申 (49)
- swagger和postman (46)
- 结构化程序设计主要强调 (53)
- 172.1 (57)
- apipostwebsocket (47)
- 唯品会后台 (61)
- 简历助手 (56)
- offshow (61)