From a82770071d229216b52828cfc048d019457e3666 Mon Sep 17 00:00:00 2001 From: Lukasz Plewa Date: Fri, 19 Feb 2021 15:39:50 +0100 Subject: [PATCH 1/3] Homework ping pong easier --- module4/exercises/03b_ping_pong_easier.cpp | 38 ++++++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/module4/exercises/03b_ping_pong_easier.cpp b/module4/exercises/03b_ping_pong_easier.cpp index f941270..1276c03 100644 --- a/module4/exercises/03b_ping_pong_easier.cpp +++ b/module4/exercises/03b_ping_pong_easier.cpp @@ -4,12 +4,13 @@ #include #include #include +#include using namespace std; class PingPong { mutex m_; condition_variable opponentsTurn_; - bool isPingTurn_ = true; + atomic_bool isPingTurn_{true}; int repetitions_; atomic play_{true}; @@ -23,12 +24,20 @@ class PingPong { while (reps < repetitions_ and play_) { unique_lock l(m_); - // TODO: wait to be used here + printing, reps incrementation, chainging turn to pong - this_thread::sleep_for(500ms); + opponentsTurn_.wait(l, [&] {return isPingTurn_.load(); }); + cout << "Ping" << endl; + reps++; + isPingTurn_=false; + opponentsTurn_.notify_all(); } if (reps >= repetitions_) { // TOOD: only print message here + std::stringstream notify; + notify << "Ping is finishing game. Num reps has reached " << reps << endl; + cout << notify.str(); + play_ = false; + opponentsTurn_.notify_all(); } } @@ -37,18 +46,33 @@ class PingPong { while (reps < repetitions_ and play_) { unique_lock l(m_); - // TODO: wait to be used here + printing, reps incrementation, chainging turn to ping - this_thread::sleep_for(500ms); + opponentsTurn_.wait(l, [&] {return !isPingTurn_.load(); }); + cout << "Pong" << endl; + reps++; + isPingTurn_=true; + opponentsTurn_.notify_all(); } if (reps >= repetitions_) { - // TODO: set play_ to false, display message, notify others to avoid deadlocks + std::stringstream notify; + notify << "Ping is finishing game. Num reps has reached " << reps << endl; + cout << notify.str(); + play_ = false; + opponentsTurn_.notify_all(); } } void stop([[maybe_unused]] chrono::seconds timeout) { unique_lock l(m_); // TODO: wait_for to be used here. Check for a return value and set play_ to false - + auto ret = opponentsTurn_.wait_for(l, timeout, [&] {return not play_.load(); }); + if (ret) { + cout << "Game finished.\n"; + } + else { + cout << "Stop is finishing game - timeout.\n"; + play_ = false; + opponentsTurn_.notify_one(); + } } }; From 45e99e9175d28a76f06ec2687eee1178efa5e796 Mon Sep 17 00:00:00 2001 From: Lukasz Plewa Date: Fri, 19 Feb 2021 15:39:50 +0100 Subject: [PATCH 2/3] Homework ping pong easier --- module4/exercises/03b_ping_pong_easier.cpp | 36 +++++++++++++++++----- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/module4/exercises/03b_ping_pong_easier.cpp b/module4/exercises/03b_ping_pong_easier.cpp index f941270..8b199ee 100644 --- a/module4/exercises/03b_ping_pong_easier.cpp +++ b/module4/exercises/03b_ping_pong_easier.cpp @@ -4,6 +4,7 @@ #include #include #include +#include using namespace std; class PingPong { @@ -23,12 +24,17 @@ class PingPong { while (reps < repetitions_ and play_) { unique_lock l(m_); - // TODO: wait to be used here + printing, reps incrementation, chainging turn to pong - this_thread::sleep_for(500ms); + opponentsTurn_.wait(l, [&] {return isPingTurn_; }); + cout << "Ping" << endl; + reps++; + isPingTurn_=false; + opponentsTurn_.notify_all(); } if (reps >= repetitions_) { - // TOOD: only print message here + std::stringstream notify; + notify << "Ping is finishing game. Num reps has reached " << reps << endl; + cout << notify.str(); } } @@ -37,18 +43,34 @@ class PingPong { while (reps < repetitions_ and play_) { unique_lock l(m_); - // TODO: wait to be used here + printing, reps incrementation, chainging turn to ping - this_thread::sleep_for(500ms); + opponentsTurn_.wait(l, [&] {return !isPingTurn_; }); + cout << "Pong" << endl; + reps++; + isPingTurn_=true; + if (reps >= repetitions_) { + play_ = false; + opponentsTurn_.notify_all(); } if (reps >= repetitions_) { - // TODO: set play_ to false, display message, notify others to avoid deadlocks + play_ = false; + std::stringstream notify; + notify << "Ping is finishing game. Num reps has reached " << reps << endl; + cout << notify.str(); } } void stop([[maybe_unused]] chrono::seconds timeout) { unique_lock l(m_); // TODO: wait_for to be used here. Check for a return value and set play_ to false - + auto ret = opponentsTurn_.wait_for(l, timeout, [&] {return not play_.load(); }); + if (ret) { + cout << "Game finished.\n"; + } + else { + cout << "Stop is finishing game - timeout.\n"; + play_ = false; + opponentsTurn_.notify_one(); + } } }; From b4ce007cd732fae9c6af9337f6e5893996dca58b Mon Sep 17 00:00:00 2001 From: Lukasz Plewa Date: Mon, 22 Feb 2021 10:05:16 +0100 Subject: [PATCH 3/3] Module4 homework ping pong - print reps --- module4/exercises/03b_ping_pong_easier.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/module4/exercises/03b_ping_pong_easier.cpp b/module4/exercises/03b_ping_pong_easier.cpp index 063594f..9deb887 100644 --- a/module4/exercises/03b_ping_pong_easier.cpp +++ b/module4/exercises/03b_ping_pong_easier.cpp @@ -25,9 +25,9 @@ class PingPong { { unique_lock l(m_); opponentsTurn_.wait(l, [&] {return isPingTurn_; }); - cout << "Ping" << endl; + cout << "Ping " << reps << endl; reps++; - isPingTurn_=false; + isPingTurn_ = false; opponentsTurn_.notify_all(); } if (reps >= repetitions_) @@ -44,16 +44,17 @@ class PingPong { { unique_lock l(m_); opponentsTurn_.wait(l, [&] {return !isPingTurn_; }); - cout << "Pong" << endl; + cout << "Pong " << reps << endl; reps++; - isPingTurn_=true; + isPingTurn_ = true; if (reps >= repetitions_) { play_ = false; + } opponentsTurn_.notify_all(); } if (reps >= repetitions_) { std::stringstream notify; - notify << "Ping is finishing game. Num reps has reached " << reps << endl; + notify << "Pong is finishing game. Num reps has reached " << reps << endl; cout << notify.str(); } }