Skip to content

Instantly share code, notes, and snippets.

@boxdot
Created May 11, 2016 07:33
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 boxdot/50bfe1a38f7acbb5b415fa398c92c1c9 to your computer and use it in GitHub Desktop.
Save boxdot/50bfe1a38f7acbb5b415fa398c92c1c9 to your computer and use it in GitHub Desktop.
Thread local test on mac
// 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