Created
September 28, 2018 18:17
-
-
Save drodil/f30a1814895b09ff3177d9ffa5d82cc1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#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