Код: Выделить всё
#include
#include
#include
#include
#include
using namespace std;
class Monitor
{
private:
string line = "";
mutex mtx;
condition_variable cv;
int vowel_count = 0;
int max_count = 0;
public:
void AddA(char symbol)
{
unique_lock lock(mtx);
cv.wait(lock, [this] { return vowel_count < 3; });
line += symbol;
vowel_count++;
max_count++;
cv.notify_all();
}
void AddBC(char symbol)
{
unique_lock lock(mtx);
cv.wait(lock, [this] { return vowel_count >= 3; });
line += symbol;
max_count++;
vowel_count = 0;
cv.notify_all();
}
string getLine()
{
unique_lock lock(mtx);
return line;
}
int getVowelCount()
{
unique_lock lock(mtx);
return vowel_count;
}
int getMaxCount()
{
unique_lock lock(mtx);
return max_count;
}
};
void First(Monitor& monitor)
{
while (true)
{
monitor.AddA('A');
if (monitor.getMaxCount() == 15)
{
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
void Second(Monitor& monitor)
{
while (true)
{
monitor.AddBC('B');
if (monitor.getMaxCount() == 15)
{
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
void Third(Monitor& monitor)
{
while (true)
{
monitor.AddBC('C');
if (monitor.getMaxCount() == 15)
{
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
void Print(Monitor& monitor)
{
while (true)
{
cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79131366/c-monitor-mutex-thread-issue[/url]