July 2017
Intermediate to advanced
244 pages
6h 33m
English
Each thread has an identifier associated with it. This ID, or handle, is a unique identifier provided by the STL implementation. It can be obtained by calling the get_id() function of the thread class instance, or by calling std::this_thread::get_id() to get the ID of the thread calling the function:
#include <iostream> #include <thread> #include <chrono> #include <mutex> std::mutex display_mutex; void worker() { std::thread::id this_id = std::this_thread::get_id(); display_mutex.lock(); std::cout << "thread " << this_id << " sleeping...\n"; display_mutex.unlock(); std::this_thread::sleep_for(std::chrono::seconds(1)); } int main() { std::thread t1(worker); std::thread::id t1_id = t1.get_id(); std::thread t2(worker); std::thread::id ...Read now
Unlock full access