CPPPROGLIB Telegram 6138
🍬 Потоки могут общаться через condition_variable

Чтобы один поток ожидал другого можно воспользоваться condition_variable.


🍴 Пошаговое решение:

1️⃣ Создайте condition_variable и mutex
2️⃣ Поток ждёт через wait()
3️⃣ Другой поток сигналит через notify_one/notify_all

#include <condition_variable>
#include <mutex>
#include <thread>
#include <iostream>

std::mutex mtx;
std::condition_variable cv;
bool ready = false;

void worker() {
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, []{ return ready; }); // Ждём сигнала

std::cout << "Worker started!\n";
}

int main() {
std::thread t(worker);

std::this_thread::sleep_for(std::chrono::seconds(1));

{
std::lock_guard<std::mutex> lock(mtx);
ready = true;
}
cv.notify_one(); // Будим поток

t.join();
return 0;
}


❗️ Частая ошибка: Забыть проверять условие в wait()

💡 Совет: Всегда передавайте предикат в wait()


Библиотека C/C++ разработчика

#буст
Please open Telegram to view this post
VIEW IN TELEGRAM
3👍1🙏1



tgoop.com/cppproglib/6138
Create:
Last Update:

🍬 Потоки могут общаться через condition_variable

Чтобы один поток ожидал другого можно воспользоваться condition_variable.


🍴 Пошаговое решение:

1️⃣ Создайте condition_variable и mutex
2️⃣ Поток ждёт через wait()
3️⃣ Другой поток сигналит через notify_one/notify_all

#include <condition_variable>
#include <mutex>
#include <thread>
#include <iostream>

std::mutex mtx;
std::condition_variable cv;
bool ready = false;

void worker() {
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, []{ return ready; }); // Ждём сигнала

std::cout << "Worker started!\n";
}

int main() {
std::thread t(worker);

std::this_thread::sleep_for(std::chrono::seconds(1));

{
std::lock_guard<std::mutex> lock(mtx);
ready = true;
}
cv.notify_one(); // Будим поток

t.join();
return 0;
}


❗️ Частая ошибка: Забыть проверять условие в wait()

💡 Совет: Всегда передавайте предикат в wait()


Библиотека C/C++ разработчика

#буст

BY Библиотека C/C++ разработчика | cpp, boost, qt


Share with your friend now:
tgoop.com/cppproglib/6138

View MORE
Open in Telegram


Telegram News

Date: |

Hui said the time period and nature of some offences “overlapped” and thus their prison terms could be served concurrently. The judge ordered Ng to be jailed for a total of six years and six months. Your posting frequency depends on the topic of your channel. If you have a news channel, it’s OK to publish new content every day (or even every hour). For other industries, stick with 2-3 large posts a week. Deputy District Judge Peter Hui sentenced computer technician Ng Man-ho on Thursday, a month after the 27-year-old, who ran a Telegram group called SUCK Channel, was found guilty of seven charges of conspiring to incite others to commit illegal acts during the 2019 extradition bill protests and subsequent months. Telegram message that reads: "Bear Market Screaming Therapy Group. You are only allowed to send screaming voice notes. Everything else = BAN. Text pics, videos, stickers, gif = BAN. Anything other than screaming = BAN. You think you are smart = BAN. How to Create a Private or Public Channel on Telegram?
from us


Telegram Библиотека C/C++ разработчика | cpp, boost, qt
FROM American