From 26c88bb65fe4f2702f0d56099d4fac0bc8bd35d9 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Tue, 30 Mar 2021 16:47:22 +0000 Subject: [PATCH] Add mutex --- UML1/superpooh.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/UML1/superpooh.cpp b/UML1/superpooh.cpp index 849d32c..f946f0b 100644 --- a/UML1/superpooh.cpp +++ b/UML1/superpooh.cpp @@ -5,6 +5,7 @@ #include #include #include +#include using namespace std; @@ -96,6 +97,8 @@ ECmd cmd = ENone; int counter = 0; +std::mutex mtx; + float targetH = 10.; float getHZadFromUser() @@ -240,9 +243,9 @@ int model() while (cmd != EExit) { - + mtx.lock(); hZad = targetH; // Неблокирующая функция для получения высоты - + mtx.unlock(); if (abs(superpooh.getH() - hZad) < 1 && abs(t - lastT) > 5) { superpooh.eat(0.1); lastT = t; @@ -277,15 +280,12 @@ int main() { boost::asio::ip::udp::socket socket(io_context, reciever); std::thread th(model); - char ch; while (cmd != EExit) { boost::array recv_b; boost::asio::ip::udp::endpoint remote_endpoint; size_t len = socket.receive_from(boost::asio::buffer(recv_b), remote_endpoint); - ch = *(recv_b.data()); std::cout.write(recv_b.data(), len); - std::cout << ch << std::endl; if (*recv_b.data() == 'q') { std::cout << "Exiting!" << std::endl; cmd = EExit; @@ -293,11 +293,15 @@ int main() { } if (*recv_b.data() == 'd') { std::cout << "Going down!" << std::endl; + mtx.lock(); targetH = 0.; + mtx.unlock(); } if (*recv_b.data() == 'u') { std::cout << "Going up to 30 meters!" << std::endl; + mtx.lock(); targetH = 30.; + mtx.unlock(); } } }