Created
May 11, 2016 07:33
-
-
Save boxdot/50bfe1a38f7acbb5b415fa398c92c1c9 to your computer and use it in GitHub Desktop.
Thread local test on mac
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
// Compile with: c++ -std=c++1y -stdlib=libc++ -pthread thread_loc.cpp | |
#include <thread> | |
#include <future> | |
#include <iostream> | |
#include <mutex> | |
namespace { | |
__thread size_t a = 0; | |
} // anonymous | |
int main(int argc, char const *argv[]) | |
{ | |
std::mutex mut; | |
auto fut1 = std::async(std::launch::async, [&mut]() { | |
std::lock_guard<std::mutex> lock(mut); | |
std::cout << "Th 1: " << ++a << std::endl; | |
}); | |
auto fut2 = std::async(std::launch::async, [&mut]() { | |
std::lock_guard<std::mutex> lock(mut); | |
std::cout << "Th 2: " << ++a << std::endl; | |
}); | |
fut1.get(); | |
fut2.get(); | |
std::cout << "Main: " << a << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment