1
0

c11threadlocal.cpp 498 B

123456789101112131415161718192021222324252627282930313233
  1. #include <thread>
  2. #include <chrono>
  3. #include <iostream>
  4. thread_local int g_mydata = 1;
  5. void thread_func1()
  6. {
  7. while (true)
  8. {
  9. ++g_mydata;
  10. }
  11. }
  12. void thread_func2()
  13. {
  14. while (true)
  15. {
  16. std::cout << "g_mydata = " << g_mydata << ", ThreadID = " << std::this_thread::get_id() << std::endl;
  17. std::this_thread::sleep_for(std::chrono::seconds(1));
  18. }
  19. }
  20. int main()
  21. {
  22. std::thread t1(thread_func1);
  23. std::thread t2(thread_func2);
  24. t1.join();
  25. t2.join();
  26. return 0;
  27. }