std::this_thread里有 访问 当前线程的 接口函数.
Interface get_id
函数原型: thread::id get_id() noexpect; // 返回 calling thread的 thread id
thread::id 是 thread的唯一标识
#include <iostream> // std::cout
#include <thread> // std::thread, std::thread::id, std::this_thread::get_id
#include <chrono> // std::chrono::seconds
std::thread::id main_thread_id = std::this_thread::get_id();
void is_main_thread() {
if ( main_thread_id == std::this_thread::get_id() )
std::cout << "This is the main thread.\n";
else
std::cout << "This is not the main thread.\n";
}
int main()
{
is_main_thread();
std::thread th (is_main_thread);
th.join();
}
Interface yield
函数原型: void yield() noexpect;