c++ - boost 进程间IPC死锁

标签 c++ boost ipc deadlock futex

这是我关于 boost ipc 库的第二篇文章。 我面临着莫名其妙的僵局,所以我想我会探索网络上可用的一些现有示例

我目前的问题只是对提供的示例的试用@

http://en.highscore.de/cpp/boost/interprocesscommunication.html

#include <boost/interprocess/managed_shared_memory.hpp> 
#include <boost/interprocess/sync/named_mutex.hpp> 
#include <boost/interprocess/sync/named_condition.hpp> 
#include <boost/interprocess/sync/scoped_lock.hpp> 
#include <iostream> 

int main() 
{ 
   boost::interprocess::managed_shared_memory   managed_shm(boost::interprocess::open_or_create, "shm", 1024); 
   int *i = managed_shm.find_or_construct<int>("Integer")(0); 
   boost::interprocess::named_mutex named_mtx(boost::interprocess::open_or_create, "mtx"); 
   boost::interprocess::named_condition named_cnd(boost::interprocess::open_or_create, "cnd"); 
   boost::interprocess::scoped_lock<boost::interprocess::named_mutex> lock(named_mtx); 
   while (*i < 10) 
   { 
     if (*i % 2 == 0) 
   { 
     ++(*i); 
     named_cnd.notify_all(); 
     named_cnd.wait(lock); 
   } 
   else 
   { 
     std::cout << *i << std::endl; 
     ++(*i); 
     named_cnd.notify_all(); 
     named_cnd.wait(lock); 
   } 
 } 
 named_cnd.notify_all(); 
 boost::interprocess::shared_memory_object::remove("shm"); 
 boost::interprocess::named_mutex::remove("mtx"); 
 boost::interprocess::named_condition::remove("cnd"); 
} 

此示例代码导致了我的死锁。 strace 指示两个进程:

  futex(0x...,FUTEX_WAIT,1,NULL

我在 ubuntu 12.04 上用 gcc 4.7 编译

任何帮助/想法为什么会发生?

PS:请注意,如果您尝试这样做并遇到死锁,请保留一个独立程序,该程序仅在末尾执行删除命令以清除共享对象。否则 i 的计数器将从它的当前状态开始,而不是从 0 开始。

最佳答案

这是一个报告的错误,使用了一些最近的 boost 版本: https://svn.boost.org/trac/boost/ticket/7682

关于c++ - boost 进程间IPC死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15919983/

相关文章:

php - 如何从 PHP 脚本与 python 守护进程通信

python /POpen/gpg : Supply passphrase and encryption text both through stdin or file descriptor

两个进程中的多线程通过共享内存进行通信会导致速度大幅下降,然后核心映射会恢复它

c++ - 是否应该使用 unique_ptr 来更轻松地实现 "move"语义?

c++ - 使用 `std::min` 作为算法参数

c++ - 将容器存储在 boost::variant 中

c++ - Boost Shared Memory - 指针的返回 vector

c++ - vtable 在虚拟继承的情况下

c++ - 如何在每次循环迭代之前清除屏幕?

C/C++高频消息程序