#include <atomic>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <random>
#include <thread>
std::atomic<int> atomic_count{0};
std::atomic<int> atomic_writes{0};
constexpr int global_max_count{72};
constexpr int writes_per_line{8};
constexpr int max_delay{100};
template<int Max> int random_value()
{
static std::uniform_int_distribution<int> distr{1, Max};
static std::random_device engine;
static std::mt19937 noise{engine()};
static std::mutex rand_mutex;
std::lock_guard μ{rand_mutex};
return distr(noise);
}
int main()
{
auto work = [](const char id)
{
for (int count{}; (count = ++atomic_count) <= global_max_count;) {
std::this_thread::sleep_for(
std::chrono::milliseconds(random_value<max_delay>()));
bool new_line{false};
if (++atomic_writes % writes_per_line == 0) {
new_line = true;
}
// print thread `id` and `count` value
{
static std::mutex cout_mutex;
std::lock_guard m{cout_mutex};
std::cout << "[" << id << "] " << std::setw(3) << count << " │ "
<< (new_line ? "
" : "") << std::flush;
}
}
};
std::jthread j1(work, 'A'), j2(work, 'B'), j3(work, 'C'), j4(work, 'D');
}
Code Example |
---|
Cpp :: Chef and the Wildcard Matching codechef solution in c++ |
Cpp :: c++ tuple example |
Cpp :: C++ References |
Cpp :: c++ regex to validate indian phone number pattern |
Cpp :: c++ str |
Cpp :: c++ sort cout end |
Cpp :: fsafdsfdsaf |
Cpp :: std remove example |
Cpp :: c++ comments |
Cpp :: ue4 set size of widget c++ |
Cpp :: Passing a string to a function |
Cpp :: convert c program to c++ online |
Cpp :: c++ start thread later |
Cpp :: stp |
Cpp :: iff cpp |
Cpp :: convert GLFWwindow* to IntPtr |
Cpp :: composition namespaces c++ |
Cpp :: spyder enviroment |
Cpp :: castin in C++ |
Cpp :: c++ static array in Klasse |
Cpp :: sprintf add two xeroes for a float number |
Cpp :: add nested vector cpp |
Cpp :: pca compact trick |
Cpp :: icon on win32 button |
Cpp :: c++ rainbow text |
Cpp :: arraylist equivalent cpp |
Cpp :: how to pass arrays by reference c++ |
Cpp :: stack in c++ |
Cpp :: what do we use c++ vectors for |
Cpp :: string array 2d c++ |