Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Runner-mt
#ifndef RUNNER_H #define RUNNER_H //#include <unistd.h> // usleep extern int activeRunner ; extern int countRunners ; class Runner { int m_Nr; void local_sec(); void critical_sec(); template<typename Fun> void await(Fun predicate); public: Runner(int n) :m_Nr(n){ ++countRunners; } ~Runner() { --countRunners; } void operator()(); }; #endif /// Runner.cpp #include "stdafx.h" #include "Runner.h" #include <iostream> #include <thread> #include <string> #include <mutex> int activeRunner = 0; int countRunners = 0; void Runner::operator()() { while (true) { await([this](){ return activeRunner == m_Nr; }); local_sec(); static std::mutex mtx; std::lock_guard<std::mutex> lck(mtx); critical_sec(); ++activeRunner; if (activeRunner >= countRunners) activeRunner = 0; } } template<typename Fun> void Runner::await(Fun predicate) { while (!predicate()) { std::this_thread::sleep_for(std::chrono::milliseconds(200)); } } void Runner::critical_sec() { std::cout << "Working Runner: " << m_Nr << " thread id: " << std::this_thread::get_id() << std::endl; } void Runner::local_sec() { std::this_thread::sleep_for(std::chrono::milliseconds(250)); } int main() { Runner R0(0); Runner R1(1); R0(); R1(); return 0; }
run
|
edit
|
history
|
help
0
VS struct name enumerate
c++
c++functor
postfix eval in c++ - multidigits decimal
a parameter pack
vaska
DCapSurfaceDesc
ㅇㅇ
class with unique_ptr to vector
Not Common Numbers