gpt4 book ai didi

c++ - 使用具有相同条件变量的不同互斥体是否有意义?

转载 作者:行者123 更新时间:2023-12-03 08:17:39 26 4
gpt4 key购买 nike

条件变量的notify_one()函数的文档位于cppreference.com指出以下内容

The notifying thread does not need to hold the lock on the same mutex as the one held by the waiting thread(s); in fact doing so is a pessimization, since the notified thread would immediately block again, waiting for the notifying thread to release the lock.

这句话的第一部分很奇怪,如果我在通知通知线程中持有不同的互斥体,那么互斥体没有真正的意义,因为没有此处“阻止”操作。事实上,如果持有不同的互斥锁,那么虚假唤醒就有可能导致通知丢失!我的印象是,在这种情况下我们最好不要锁定通知线程。有人可以澄清一下吗?

请考虑 cppreference 页面中关于条件变量的以下内容作为示例。

std::mutex m;    // this is supposed to be a pessimization
std::condition_variable cv;
std::string data;
bool ready = false;
bool processed = false;

void worker_thread()
{
// Wait until main() sends data
std::unique_lock<std::mutex> lk(m); // a different, local std::mutex is supposedly better
cv.wait(lk, []{return ready;});

// after the wait, we own the lock.
std::cout << "Worker thread is processing data\n";
data += " after processing";

// Send data back to main()
processed = true;
std::cout << "Worker thread signals data processing completed\n";

lk.unlock();
cv.notify_one();
}

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

data = "Example data";
// send data to the worker thread
{
std::lock_guard<std::mutex> lk(m); // a different, local std::mutex is supposedly better
ready = true;
std::cout << "main() signals data ready for processing\n";
}
cv.notify_one();

// wait for the worker
{
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, []{return processed;});
}
std::cout << "Back in main(), data = " << data << '\n';

worker.join();
}
PS。我看到了一些标题相似的问题,但它们涉及问题的不同方面。

最佳答案

我认为 cppreference 的措辞在这里有些尴尬。我认为他们只是试图将与条件变量结合使用的互斥体与其他不相关的互斥体区分开来。

使用具有不同互斥锁的条件变量是没有意义的。互斥锁用于对实际语义条件(在示例中它只是变量 ready )进行原子性更改,因此每当更新或检查条件时都必须保留它。此外,还需要确保未阻塞的等待线程可以立即检查条件,而不会再次遇到竞争条件。

我的理解是这样的:当 notify_one 时,不持有与条件变量关联的互斥锁是可以的。被调用,或者任何互斥体,但是出于不同原因保留其他互斥体是可以的。

悲观的不是只使用一个互斥锁,而是当你知道另一个线程在收到通知后应该立即尝试获取互斥锁时,持有该互斥锁的时间超过了必要的时间。

我认为我的解释与 cppreference on condition variable 中给出的解释一致:

The thread that intends to modify the shared variable has to

acquire a std::mutex (typically via std::lock_guard)

perform the modification while the lock is held

execute notify_one or notify_all on the std::condition_variable (the lock does not need to be held for notification)

Even if the shared variable is atomic, it must be modified under the mutex in order to correctly publish the modification to the waiting thread.

Any thread that intends to wait on std::condition_variable has toacquire a std::unique_lock<std::mutex>, on the same mutex as used to protect the shared variable

此外standard明确禁止对 wait 使用不同的互斥锁, wait_­for ,或wait_­until :

lock.mutex() returns the same value for each of the lock arguments supplied by all concurrently waiting (via wait, wait_­for, or wait_­until) threads.

关于c++ - 使用具有相同条件变量的不同互斥体是否有意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68866142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com