<thread> 函数

标头 <thread> 提供以下函数:

get_id

唯一标识当前的执行线程。

thread::id get_id() noexcept;

返回值

一个类型 thread::id 唯一标识当前执行线程的对象。

Example

#include <iostream>
#include <thread>

int main()
{
    std::thread::id current_thread_id = std::this_thread::get_id();
    std::cout << "Current thread id: " << current_thread_id;
}
Current thread id: 16196

sleep_for

阻止调用线程。

template <class Rep, class Period>
void sleep_for(const chrono::duration<Rep, Period>& Rel_time);

参数

Rel_time
一个 duration 指定时间间隔的对象。

备注

该函数至少在指定的 Rel_time时间阻止调用线程。 此函数不引发任何异常。

sleep_until

阻止调用线程,至少直到指定的时间。

template <class Clock, class Duration>
void sleep_until(const chrono::time_point<Clock, Duration>& Abs_time);

参数

Abs_time
表示时间点。

备注

此函数不引发任何异常。

swap

交换两个 thread 对象的状态。

void swap(thread& Left, thread& Right) noexcept;

参数

Left
左 thread 对象。

Right
正确的 thread 对象。

备注

函数调用 Left.swap(Right)。

yield

表示要运行其他线程的操作系统,即使当前线程会照常继续运行。

inline void yield() noexcept;

另请参阅

<thread>