solaris上的boost库信号量

boost library semaphores on solaris

本文关键字:信号量 boost 上的 solaris      更新时间:2023-10-16

我在RHEL中使用boost信号量,目前我正在将代码移植到solaris 10。我遇到了一个奇怪的问题,即提升信号量不能正常工作。

我使用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();
...

系统信息:

solaris 10

gcc:4.1.2使用binutils 2.18 自行构建

提高1.47

sparc体系结构

这完全与信号量和solaris实现的使用有关。在我的案例中,进程1在进程2打开信号量的共享内存之前发布。因此,流程二没有从流程一得到任何帖子。我得到了上面的代码,下面列出了一些小的更改:

...
//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();
...