thread_local — variable has separate copy per thread.
1thread_local int counter = 0;23void increment() {4 counter++; // each thread has its own counter5}67std::thread t1(increment);8std::thread t2(increment);9t1.join(); t2.join();10// counter is still 0 in main thread
Use cases:
Lifetime:
Warning:
std::shared_ptr across threads