Spin-lock 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.
Spin-lock synchronization primitive.
Spin-lock synchronization primitive definition.