Created
August 30, 2019 09:28
-
-
Save danielhams/a0859edf5782c1cc3f06b2bd46023ee9 to your computer and use it in GitHub Desktop.
More complex example of thread local storage in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <thread> | |
#include <mutex> | |
#include <iostream> | |
std::mutex log_mutex; | |
thread_local int tl=0; | |
void logit( int num, const char * val ) | |
{ | |
log_mutex.lock(); | |
std::cout << num << " " << tl << " " << val << std::endl; | |
std::cout << num << " " << tl << " " << val << std::endl; | |
log_mutex.unlock(); | |
} | |
void doit( int num ) | |
{ | |
logit(num, "apple"); | |
tl = num; | |
logit(num, "banana"); | |
} | |
int main() | |
{ | |
std::thread t1 = std::thread(doit,1); | |
std::thread t2 = std::thread(doit,2); | |
t1.join(); | |
t2.join(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment