【问题标题】:boost library semaphores on solaris在 solaris 上提升库信号量
【发布时间】:2013-02-22 20:47:03
【问题描述】:

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

我使用 boost 的 website 上的示例创建了匿名信号量。信号量在开发机器上工作正常,但无法在测试机器上运行。一个进程在发布到另一个进程后卡在等待状态,但另一个进程尚未退出等待状态。

这是我的信号量减速:

...
//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 架构

【问题讨论】:

  • Boost 的哪个版本?两台机器上的版本一样吗?
  • 经过一点尝试和跟踪,我发现如果我在进程 1 中发布两次,那么它在测试机器上工作正常,但是它在开发机器上总是失败。但它在测试机器上仍然有时会失败。
  • @JoachimPileborg 感谢您的评论!

标签: c++ boost solaris semaphore solaris-10


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多