#include <Windows.h>
#include <iostream>
#include <thread>
std::thread::id master_thread_id;
void do_master_thread_work() {
std::cout << __FUNCTION__ << ": " << std::this_thread::get_id() << "\n";
}
void do_common_work() {
std::cout << __FUNCTION__ << ": "<<std::this_thread::get_id() <<"\n";
}
void some_core_part_of_algorithm()
{
while (1) {
// 识别当前线程是否为主线程, 如果是则进行额外工作
if (std::this_thread::get_id() == master_thread_id)
{
do_master_thread_work();
}
do_common_work();
Sleep(1000);
}
}
int main() {
std::thread master_thread = std::thread(some_core_part_of_algorithm);
// 这个id只能在绑定线程函数后获取
master_thread_id = master_thread.get_id();
Sleep(1000);
std::thread common_thread = std::thread(some_core_part_of_algorithm);
return 0;
}