Mac OS X:提升进程间信号量timed_wait:CPU消耗异常

时间:2010-07-09 12:30:11

标签: boost macos semaphore interprocess

将代码段从Windows移植到Mac OS X后,我发现它在运行时占用了整个CPU内核;对CPU消耗的负责任调用是boost :: interprocess :: interprocess_semaphore :: timed_wait。

以下是重现此行为的代码部分。

#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>

#include <boost/thread/thread_time.hpp>

#include <iostream>

static bool gStopRequested(false);

struct ShmObj
{
    boost::interprocess::interprocess_semaphore mSemaphore;
    ShmObj() : mSemaphore(0) {};
    ~ShmObj() {};
};

int main(char* argc, const char** argv)
{
    boost::interprocess::shared_memory_object* lShmObj = NULL;
    std::string lShmObjName("My_Boost_Interprocess_Test");
    boost::interprocess::mapped_region* lRegion;
    ShmObj* lObj;

    //Create shared segment
    try
    {
        lShmObj = new boost::interprocess::shared_memory_object(boost::interprocess::create_only, lShmObjName.c_str(), boost::interprocess::read_write);
    }
    catch (boost::interprocess::interprocess_exception &ex)
    {
        if (ex.get_error_code() != boost::interprocess::already_exists_error)
        {
            std::cerr << "Some error" << std::endl;
            exit(1);
        }
        else
        {
            std::cerr << "Already exists, just taking it back." << std::endl;
            try
            {
                lShmObj = new boost::interprocess::shared_memory_object(boost::interprocess::open_only, lShmObjName.c_str(), boost::interprocess::read_write);
            }
            catch (boost::interprocess::interprocess_exception &ex2)
            {
                std::cerr << "D'oh !" << std::endl;
                exit(1);
            }
        }
    }
    if (!lShmObj)
    {
        exit(1);
    }
    lShmObj->truncate(sizeof(ShmObj));
    lRegion = new boost::interprocess::mapped_region(*lShmObj, boost::interprocess::read_write);
    lObj = new (lRegion->get_address()) ShmObj;

    // The loop
    while (!gStopRequested)
    {
        boost::system_time lDeadlineAbsoluteTime = boost::get_system_time() + boost::posix_time::milliseconds(500);

        if (lObj->mSemaphore.timed_wait(lDeadlineAbsoluteTime))
        {
            std::cout << "acquired !" << std::endl;
        }
        else
        {
            std::cout << "tick" << std::endl;
        }
    }
}

然后,我读到在Mac OS X下无法使用未命名的信号量,所以我认为可能是因为未命名的信号量未被有效模拟...我接着尝试了以下,不成功:

#include <boost/interprocess/sync/named_semaphore.hpp>
#include <boost/thread/thread_time.hpp>

#include <iostream>

static bool gStopRequested(false);

int main(char* argc, const char** argv)
{
    boost::interprocess::named_semaphore::remove("My_Boost_Interprocess_Test");
    boost::interprocess::named_semaphore lMySemaphore(boost::interprocess::open_or_create, "My_Boost_Interprocess_Test", 1);

    // The loop
    while (!gStopRequested)
    {
        boost::system_time lDeadlineAbsoluteTime = boost::get_system_time() + boost::posix_time::milliseconds(500);

        if (lMySemaphore.timed_wait(lDeadlineAbsoluteTime))
        {
            std::cout << "acquired !" << std::endl;
        }
        else
        {
            std::cout << "tick" << std::endl;
        }
    }
}

由于可用的Posix原语,我实际上期待在Mac OS X上更好的boost :: interprocess行为,但实际上并非如此。有任何解决方案吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

我成功使用了Mach信号量而不是boost :: interprocess ...请参阅http://pkaudio.blogspot.com/2010/05/mac-os-x-no-timed-semaphore-waits.html

相关问题