Skip to content

Instantly share code, notes, and snippets.

@danielhams
Created August 30, 2019 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielhams/a0859edf5782c1cc3f06b2bd46023ee9 to your computer and use it in GitHub Desktop.
Save danielhams/a0859edf5782c1cc3f06b2bd46023ee9 to your computer and use it in GitHub Desktop.
More complex example of thread local storage in c++
#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