Skip to content

Instantly share code, notes, and snippets.

@jiripospisil
Created March 5, 2013 09:57
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 jiripospisil/5089197 to your computer and use it in GitHub Desktop.
Save jiripospisil/5089197 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <thread>
int main(int argc, const char** argv)
{
std::mutex value_mutex;
std::vector<std::thread> threads;
int value = 0;
for (int i = 1; i <= 10000; ++i) {
threads.push_back(std::thread([&value, &value_mutex] (const int i) {
std::lock_guard<std::mutex> lg(value_mutex);
value += i;
}, i));
}
for (auto& t: threads) t.join();
std::cout << value << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment