我在 RHEL 中使用 boost 信号量,目前我正在将代码移植到 solaris 10。我遇到了一个奇怪的问题,即 boost 信号量无法正常工作。
我使用 boost网站上的示例创建了匿名信号量。信号量在开发机器上工作正常,但无法在测试机器上运行。一个进程在发布到另一个进程后卡在等待状态,但另一个进程尚未退出等待状态。
这是我的信号量减速:
...
//in global space
struct iSema
{
interprocess_semaphore ASync;
interprocess_semaphore BSync;
iSema()
:ASync(0), BSync(0)
{}
}*m_Sema;
mapped_region SemaRegion;
#define SHM_SIZE 512
...
...
//in main process 1
try
{
std::size_t ShmSize = SHM_SIZE;
shared_memory_object::remove("xyz"); //remove previous instance
shared_memory_object shm(create_only, "xyz", read_write); //create new
shm.truncate(sizeof(struct iSema));
mapped_region region(shm, read_write); //get into local scope region
SemaRegion.swap(region); //swap with global scope region
m_Sema = new (SemaRegion.get_address()) (struct iSema); //map it
}
catch(exception& e)
{//logging
}
...
//Do some thing
m_Sema->ASync.post();
m_Sema->BSync.wait();//stuck at this place
...
...
//in main second process
try
{
std::size_t ShmSize = SHM_SIZE;
shared_memory_object shm(open_only, "xyz", read_write);
shm.truncate(sizeof(struct iSema));
mapped_region region(shm, read_write);
SemaRegion.swap(region);
m_Sema = new (SemaRegion.get_address()) (struct iSema);
}
catch(exception& e)
{
//logging
}
m_Sema->ASync.wait();
m_Sema->BSync.post();
...
系统信息:
太阳能 10
gcc:4.1.2 使用 binutils 2.18 自构建
提升 1.47
sparc架构