接前一篇文章:C++常用容器、函数、类集(1)
本文内容参考:
C++ std::thread线程详解-腾讯云开发者社区-腾讯云
特此致谢!
5. std::thread
std::thread是C++11标准库(STL)提供的多线程编程组件,表示单个线程的线程类,封装了操作系统线程接口,用于实现并发执行。其核心功能包括线程创建、生命周期管理及跨平台支持,需重点关注构造函数使用、join/detach机制和同步问题。
(1)创建并启动一个线程
要启动线程,只需要创建一个新的线程对象,并将要调用的执行代码(即可调用对象)传递到对象的构造函数中。
代码示例:
//当程序到达此行时,将在后台启动任务以运行aFunction
//t:线程对象
//aFunction: 任务或线程执行
std::thread t(aFunction);
创建对象后,将启动一个新线程,该线程将执行aFunction中指定的代码。可调用对象可以是以下5个项中的任何一个:
- 函数指针
- Lambda 表达式
- 函数对象
- 非静态成员函数
- 静态成员函数
与5种形式对应的代码示例:
/*****************1.使用函数指针启动线程********************/
//函数指针可以是可调用对象,传递给 std::thread 构造函数以初始化线程。
void foo(params)
{
...
}
// The parameters to the function are put after the comma
std::thread thread_obj(foo, params);
/********************************************************/
/***************2.使用 Lambda 表达式启动线程****************/
//定义一个lambda表达式
auto f = [](params)
{
...
};
//使用 lambda 表达式作为可调用对象来启动
std::thread thread_object(f, params);
/********************************************************/
/******************3.使用函数对象启动线程*******************/
// 定义一个函数对象
class fn_object_class {
// 重载operator()
void operator()(params)
{
...
}
}
std::thread thread_object(fn_object_class(), params);
/********************************************************/
/***************4.使用非静态成员函数启动线程****************/
// 定义一个类
class Base {
public:
// 非静态成员函数
void foo(params) { ... }
}
//创建Base类对象b
Base b;
// 第一个参数是类非静态成员函数的引用
// 第二个参数类对象的引用
// 第三个参数是非静态成员函数的参数
std::thread thread_obj(&Base::foo, &b, params);
/********************************************************/
/***************5.使用静态成员函数启动线程****************/
// 定义一个类
class Base {
public:
//静态成员数
static void foo(params) { ... }
}
//创建Base类对象b
Base b;
// 其一个参数是类静态成员函数的引用
// 第二个参数是该函数的参数
std::thread thread_obj(&Base::foo, params);
/********************************************************/
(2)等待线程执行完毕
线程启动后,可能需要等待线程完成,然后才能采取一些操作。要等待线程,使用std::thread::join()函数。此函数使当前线程等待,直到*this标识的线程完成执行。
代码示例:
int main()
{
//开始一个线程t1
std::thread t1(callable);
//等待线程t1执行完成
t1.join();
//t1完成后再继续执行其他语句
...
}
(3)线程对象与任务分离
线程对象可以与其任务分离,线程在后台独立运行。这对于需要在后台运行的任务非常有用,不需要在运行时停止它。分离后的线程不能再被join()。
代码示例:
#include <iostream>
#include <chrono>
#include <thread>
void independentThread()
{
std::cout << "Starting concurrent thread.\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "Exiting concurrent thread.\n";
}
void threadCaller()
{
std::cout << "Starting thread caller.\n";
std::thread t(independentThread);
t.detach();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Exiting thread caller.\n";
}
int main()
{
threadCaller();
std::this_thread::sleep_for(std::chrono::seconds(5));
}
std::thread常用成员函数
6. std::sleep_for
std::this_thread::sleep_for是C++11引入的线程控制函数,用于让当前线程暂停执行指定时长,属于非阻塞式休眠。
使当前线程暂停执行,但不释放 CPU 资源,其他线程可继续运行。暂停时间由std::chrono定义的时间间隔控制,支持毫秒、微秒等精细时间单位。
代码示例:
#include <iostream>
#include <thread>
#include <chrono>
int main()
{
std::cout << "Start waiting..." << std::endl;
// 暂停当前线程 2 秒钟
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "2 seconds have passed!" << std::endl;
return 0;
}
更多内容请看下回。