C++ solaris上的boost库信号量
我在RHEL中使用了boost信号量,目前我正在将代码移植到solaris 10。我遇到了一个奇怪的问题,即boost信号量不能正常工作 我使用boost上的示例创建了匿名信号量。信号量在dev机器上正常工作,但在测试机器上无法运行。一个进程在过帐到另一个进程后处于等待状态,但另一个进程尚未退出等待状态 这是我的信号灯:C++ solaris上的boost库信号量,c++,boost,solaris,semaphore,solaris-10,C++,Boost,Solaris,Semaphore,Solaris 10,我在RHEL中使用了boost信号量,目前我正在将代码移植到solaris 10。我遇到了一个奇怪的问题,即boost信号量不能正常工作 我使用boost上的示例创建了匿名信号量。信号量在dev机器上正常工作,但在测试机器上无法运行。一个进程在过帐到另一个进程后处于等待状态,但另一个进程尚未退出等待状态 这是我的信号灯: ... //in global space struct iSema { interprocess_semaphore ASync; inter
...
//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();
...
系统信息:
solaris 10
gcc:4.1.2使用binutils 2.18自行构建
增加1.47
sparc架构这完全与信号量的使用和solaris实现有关。在我的例子中,进程1是在进程2打开信号量共享内存之前发布的。因此,流程2没有从流程1获得任何post。我得到了上面的代码,下面列出了一些小改动:
...
//in global space
struct iSema
{
interprocess_semaphore ASync;
interprocess_semaphore BSync;
interprocess_semaphore CSync;
iSema()
:ASync(0), BSync(0), CSync(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->CSync.wait();
m_Sema->ASync.post();
m_Sema->BSync.wait();
...
...
//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->CSync.post();
m_Sema->ASync.wait();
m_Sema->BSync.post();
...
哪个版本的Boost?你们在两台机器上都有相同的版本吗?有一点偶然发现,如果我在进程1中发布两次,那么它在测试机器上运行良好,但在开发机器上总是失败。但有时在测试机上也会失败。@JoachimPileborg感谢您的评论!