#include <atomic>
#include <iostream>
#include <thread>
#include <vector>
int main(int argc, char** argv)
{
std::cout << "Press Enter to stop..." << std::endl;
int current = 0;
std::atomic<bool> stop(false);
std::vector<std::thread> producers;
for (int producer = 0; producer < 4; ++producer)
{
producers.emplace_back([&stop, ¤t, producer]()
{
while (!stop)
{
{
current = rand();
std::cout << "Produce value from thread " << producer << ": " << current << std::endl;
}
}
});
}
std::vector<std::thread> consumers;
for (int consumer = 0; consumer < 4; ++consumer)
{
consumers.emplace_back([&stop, ¤t, consumer]()
{
while (!stop)
{
{
std::cout << "Consume value in thread " << consumer << ": " << current << std::endl;
}
}
});
}
std::cin.get();
stop = true;
for (auto& producer : producers)
producer.join();
for (auto& consumer : consumers)
consumer.join();
return 0;
}
File-lock synchronization primitive.
Read locker synchronization primitive.
static void SleepFor(const Timespan ×pan) noexcept
Sleep the current thread for the given timespan.
int64_t milliseconds() const noexcept
Get total milliseconds of the current timespan.
Write locker synchronization primitive.
File-lock synchronization primitive definition.