gpt4 book ai didi

c++ - boost notify_all 上的进程间条件阻塞

转载 作者:太空狗 更新时间:2023-10-29 23:17:25 25 4
gpt4 key购买 nike

我有一个托管共享内存段,它有一个 boost::interprocess::interprocess_mutex 和一个 boost::interprocess::interprocess_condition 变量。我有 2 个进程访问共享内存,它们正在根据互斥锁和条件同步访问。我遇到过一个情况,我的第一个进程在 notify_all 方法上阻塞,最初我认为这是一个非阻塞方法,但似乎进程间条件实现了一个用于同步自身的互斥体。

我遇到这个死锁的情况是当进程 2 在等待条件时被不正常地杀死,我相信这会阻止条件互斥锁被解锁,然后当我再次运行进程 2 时它会阻塞。有没有办法在我第二次启动进程 2 时重置或清理进程间条件。

最佳答案

http://www.boost.org/doc/libs/1_48_0/boost/interprocess/sync/interprocess_mutex.hpp

你在使用定时锁定吗?

有关简单的死锁避免算法,请查看此处:Wikipedia
它适用于线程,但我相信它可以与 interprocess_locks 一起使用。

Recursively, only one thread is allowed to pass through a lock. If other threads enter the lock, they must wait until the initial thread that passed through completes n number of times. But if the number of threads that enter locking equal the number that are locked, assign one thread as the super-thread, and only allow it to run (tracking the number of times it enters/exits locking) until it completes. After a super-thread is finished, the condition changes back to using the logic from the recursive lock, and the exiting super-thread

  • sets itself as not being a super-thread

  • notifies the locker that other locked, waiting threads need to re-check this condition

If a deadlock scenario exists, set a new super-thread and follow that logic. Otherwise, resume regular locking.

请注意,上述算法无法解决活锁 情况,为防止此类行为,请尽可能使用信号量

我惊奇地发现 interprocess_mutex 不支持实现死锁避免算法,因为这些天以来,大多数互斥锁,即 std::mutexboost::mutex 已经支持.我想这是操作系统特定的限制。

For more flexibility try using a named_upgradable_mutex

使用定时锁在进程崩溃时捕获异常并删除可升级的互斥锁!。此类型还允许任一线程获得 boost 的权限!

关于c++ - boost notify_all 上的进程间条件阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18240263/

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