Checkpoint synchronization: Difference between revisions
Content added Content deleted
(Omit Axe) |
(Added C++ solution) |
||
Line 303: | Line 303: | ||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
=={{header|C++}}== |
|||
{{works with|C++11}} |
|||
<lang cpp>#include <iostream> |
|||
#include <chrono> |
|||
#include <atomic> |
|||
#include <mutex> |
|||
#include <random> |
|||
#include <thread> |
|||
std::mutex cout_lock; |
|||
class Latch |
|||
{ |
|||
std::atomic<int> semafor; |
|||
public: |
|||
Latch(int limit) : semafor(limit) {} |
|||
void wait() |
|||
{ |
|||
semafor.fetch_sub(1); |
|||
while(semafor.load() > 0) |
|||
std::this_thread::yield(); |
|||
} |
|||
}; |
|||
struct Worker |
|||
{ |
|||
static void do_work(int how_long, Latch& barrier, std::string name) |
|||
{ |
|||
std::this_thread::sleep_for(std::chrono::milliseconds(how_long)); |
|||
{ std::lock_guard<std::mutex> lock(cout_lock); |
|||
std::cout << "Worker " << name << " finished work\n"; } |
|||
barrier.wait(); |
|||
{ std::lock_guard<std::mutex> lock(cout_lock); |
|||
std::cout << "Worker " << name << " finished assembly\n"; } |
|||
} |
|||
}; |
|||
int main() |
|||
{ |
|||
Latch latch(5); |
|||
std::mt19937 rng(std::random_device{}()); |
|||
std::uniform_int_distribution<> dist(300, 3000); |
|||
std::thread threads[] { |
|||
std::thread(&Worker::do_work, dist(rng), std::ref(latch), "John"), |
|||
std::thread{&Worker::do_work, dist(rng), std::ref(latch), "Henry"}, |
|||
std::thread{&Worker::do_work, dist(rng), std::ref(latch), "Smith"}, |
|||
std::thread{&Worker::do_work, dist(rng), std::ref(latch), "Jane"}, |
|||
std::thread{&Worker::do_work, dist(rng), std::ref(latch), "Mary"}, |
|||
}; |
|||
for(auto& t: threads) t.join(); |
|||
std::cout << "Assembly is finished"; |
|||
}</lang> |
|||
{{out} |
|||
<pre> |
|||
Worker Mary finished work |
|||
Worker Smith finished work |
|||
Worker John finished work |
|||
Worker Henry finished work |
|||
Worker Jane finished work |
|||
Worker Jane finished assembly |
|||
Worker Smith finished assembly |
|||
Worker Mary finished assembly |
|||
Worker Henry finished assembly |
|||
Worker John finished assembly |
|||
Assembly is finished |
|||
</pre> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |