在 C++11 中,为了更好地支持并发编程,标准库提供了命名空间 std::this_thread
,它专门用于表示当前线程,并提供了一些常用的与线程管理相关的工具和功能。通过这些工具,我们可以更灵活地控制线程的执行顺序、获取当前线程的 ID、休眠线程等操作。
一、this_thread::get_id()
—— 获取当前线程的 ID#
函数原型:#
1
|
std::thread::id get_id() noexcept;
|
该函数用于获取调用它的线程的 ID。在多线程环境中,线程 ID 是非常有用的调试工具,可以帮助开发者标识各个线程的身份。C++11 中,std::thread
类也提供了类似的 get_id()
方法,用于获取某个线程对象的 ID。
示例:#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <iostream>
#include <thread>
using namespace std;
void func() {
cout << "子线程 ID: " << this_thread::get_id() << endl;
}
int main() {
thread t1(func);
cout << "主线程 ID: " << this_thread::get_id() << endl;
t1.join(); // 回收线程资源
}
|
输出:#
1
2
|
主线程 ID: 140710877099776
子线程 ID: 140710869462784
|
二、this_thread::sleep_for()
—— 线程休眠指定时间#
函数原型:#
1
2
|
template <class Rep, class Period>
void sleep_for (const chrono::duration<Rep,Period>& rel_time);
|
this_thread::sleep_for()
函数让当前线程休眠指定的一段时间,参数是 chrono::duration
类型。它是一个跨平台的休眠函数,和操作系统提供的 Sleep()
或 sleep()
不同,sleep_for()
提供了更强的跨平台兼容性。
示例:#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
void func() {
for (int i = 1; i <= 3; i++) {
cout << "子线程休眠 " << i << " 秒..." << endl;
this_thread::sleep_for(chrono::seconds(1));
}
}
int main() {
thread t1(func);
t1.join();
}
|
输出:#
1
2
3
|
子线程休眠 1 秒...
子线程休眠 2 秒...
子线程休眠 3 秒...
|
3. this_thread::sleep_until()
—— 线程休眠至指定时间#
函数原型:#
1
2
|
template <class Clock, class Duration>
void sleep_until (const chrono::time_point<Clock,Duration>& abs_time);
|
this_thread::sleep_until()
是一个非常强大的函数,它可以让线程休眠直到某个特定的时间点。这在实现定时任务、调度任务时特别有用。
示例:#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main() {
auto now = chrono::system_clock::now();
auto future_time = now + chrono::seconds(5);
cout << "主线程将在 5 秒后继续执行..." << endl;
this_thread::sleep_until(future_time);
cout << "主线程恢复执行。" << endl;
}
|
输出:#
1
2
|
主线程将在 5 秒后继续执行...
主线程恢复执行。
|
四、this_thread::yield()
—— 让出当前线程的 CPU 时间片#
函数原型:#
this_thread::yield()
函数允许当前线程主动让出其正在使用的 CPU 时间片,以便让其他线程有机会运行。它常用于优化多线程应用中的 CPU 资源分配,避免线程忙等待(busy waiting)。
示例:#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
void func() {
for (int i = 0; i < 3; ++i) {
cout << "子线程执行中,主动让出 CPU..." << endl;
this_thread::yield();
}
}
int main() {
thread t1(func);
t1.join();
}
|
输出:#
1
2
3
|
子线程执行中,主动让出 CPU...
子线程执行中,主动让出 CPU...
子线程执行中,主动让出 CPU...
|
五、线程类中的其他常用成员函数#
5.1 std::thread::swap()
#
swap()
函数用于交换两个线程对象的内部数据和状态。这在某些情况下用于优化线程管理,特别是需要转移线程资源所有权时。
示例:#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <iostream>
#include <thread>
using namespace std;
void task1() { cout << "任务1" << endl; }
void task2() { cout << "任务2" << endl; }
int main() {
thread t1(task1);
thread t2(task2);
t1.swap(t2); // 交换两个线程的任务
t1.join();
t2.join();
}
|
5.2 std::thread::hardware_concurrency()
#
hardware_concurrency()
返回系统中可供使用的硬件线程数量,这个值表示系统支持的并发执行线程数(通常对应 CPU 核心数)。它只是一个近似值,具体表现与硬件和操作系统有关。
示例:#
1
2
3
4
5
6
7
8
|
#include <iostream>
#include <thread>
using namespace std;
int main() {
unsigned int cores = thread::hardware_concurrency();
cout << "系统支持的并发线程数量: " << cores << endl;
}
|
输出:#
六、综合示例:使用 this_thread
和线程管理函数#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
void func(int bh, const string& str) {
cout << "子线程:" << this_thread::get_id() << endl;
for (int i = 1; i <= 3; ++i) {
cout << "第" << i << "次表白:亲爱的" << bh << "号," << str << endl;
this_thread::sleep_for(chrono::seconds(1)); // 休眠 1 秒
}
}
int main() {
// 创建两个线程执行任务
thread t1(func, 3, "我是一只傻傻鸟。");
thread t2(func, 8, "我有一只小小鸟。");
cout << "主线程:" << this_thread::get_id() << endl;
cout << "线程 t1:" << t1.get_id() << endl;
cout << "线程 t2:" << t2.get_id() << endl;
t1.join(); // 等待 t1 完成
t2.join(); // 等待 t2 完成
cout << "所有线程执行完毕。" << endl;
}
|
输出:#
1
2
3
4
5
6
7
8
9
|
主线程:140705598822144
线程 t1:140705590429440
线程 t2:140705582036736
子线程:140705590429440
第1次表白:亲爱的3号,我是一只傻傻鸟。
子线程:140705582036736
第1次表白:亲爱的8号,我有一只小小鸟。
...
所有线程执行完毕。
|