Skip to content

Instantly share code, notes, and snippets.

@drodil
Created September 28, 2018 18:17
Show Gist options
  • Save drodil/f30a1814895b09ff3177d9ffa5d82cc1 to your computer and use it in GitHub Desktop.
Save drodil/f30a1814895b09ff3177d9ffa5d82cc1 to your computer and use it in GitHub Desktop.
#include <vector>
std::size_t counter = 0;
std::mutex cout_mutex;
void foo()
{
{
std::lock_guard<std::mutex> lock(cout_mutex);
std::cout << "T" << get_thread_id() << " increasing counter" << std::endl;
}
counter++;
}
int main()
{
std::vector<std::thread> v;
for(int n = 0; n < 2; ++n) {
v.emplace_back(foo);
}
for(auto& t : v) {
t.join();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment