1

这是我与 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 开始。

4

2 回答 2

0

I think it is clear: according to the documentation, notify methods have ho effect, unless there is already a thread (or more) waiting. And in the provided example it can occur, that all processes execute notify_all() before any of them reaches wait().

于 2013-12-02T08:21:40.463 回答
0

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

于 2013-05-03T19:45:53.763 回答