Программы на C++. Форум разработчиков
Anonymous
Проблема с потоком мьютекса C++ Monitor
Сообщение
Anonymous » 28 окт 2024, 05:45
Этот код должен останавливаться на 15 символах (строке), но этого не происходит, и он продолжается бесконечно. Основная проблема этого кода заключается в том, что логика условной переменной не позволяет всем потокам распознавать достижение целевого значения max_count, равного 15, в результате чего программа работает бесконечно.
Код: Выделить всё
#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]
1730083559
Anonymous
Этот код должен останавливаться на 15 символах (строке), но этого не происходит, и он продолжается бесконечно. Основная проблема этого кода заключается в том, что логика условной переменной не позволяет всем потокам распознавать достижение целевого значения max_count, равного 15, в результате чего программа работает бесконечно. [code]#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]