Mutex synchronization primitive example
#include <atomic>
#include <iostream>
#include <thread>
#include <vector>
int main(int argc, char** argv)
{
std::cout << "Press Enter to stop..." << std::endl;
std::atomic<bool> stop(false);
std::vector<std::thread> threads;
for (int thread = 0; thread < 4; ++thread)
{
threads.emplace_back([&lock, &stop, thread]()
{
while (!stop)
{
std::cout << "Random value from thread " << thread << ": " << rand() << std::endl;
}
});
}
std::cin.get();
stop = true;
for (auto& thread : threads)
thread.join();
return 0;
}
Locker synchronization primitive.
Mutex synchronization primitive.
Mutex synchronization primitive definition.