gpt4 book ai didi

c++ - boost相当于windows mutex

转载 作者:太空宇宙 更新时间:2023-11-04 16:19:41 25 4
gpt4 key购买 nike

在 Mac OS 的 boost land 中,以下代码会自行死锁:

boost::mutex m;

m.lock();
m.lock();

同理

boost::mutex m;

boost::mutex::scoped_lock lock(m);
boost::mutex::scoped_lock lock(m);

在 Windows 领域,只要释放计数相同,同一个线程可以根据需要随时获取 Win32 互斥量。我需要 boost 同步对象的确切行为。

最佳答案

您需要的是 boost::recursive_mutex :

The recursive_mutex class uses a Recursive locking strategy, so attempts to recursively lock a recursive_mutex object succeed and an internal "lock count" is maintained. Attempts to unlock a recursive_mutex object by threads that don't own a lock on it result in undefined behavior.


请注意 boost::mutex :

The mutex class uses an Unspecified locking strategy, so attempts to recursively lock a mutex object or attempts to unlock one by threads that don't own a lock on it result in undefined behavior. This strategy allows implementations to be as efficient as possible on any given platform.


在计算机科学中,递归互斥量称为 Reentrant Mutex :

In computer science, a reentrant mutex is a mutual exclusion, recursive lock mechanism. In a reentrant mutex, the same thread can acquire the lock multiple times. However, the lock must be released the same number of times or else other threads will be unable to acquire the lock

关于c++ - boost相当于windows mutex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18592720/

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