Mac OS X:boost进程间信号量计时\u等待:异常CPU消耗

Mac OS X:boost进程间信号量计时\u等待:异常CPU消耗,boost,macos,semaphore,interprocess,Boost,Macos,Semaphore,Interprocess,在将一段代码从Windows移植到MacOSX之后,我发现它在运行时消耗了整个CPU核心;CPU消耗的负责调用是boost::进程间::进程间信号量::定时等待 下面是重现这种行为的代码部分 #include <boost/interprocess/sync/interprocess_semaphore.hpp> #include <boost/interprocess/shared_memory_object.hpp> #include <boost/interp

在将一段代码从Windows移植到MacOSX之后,我发现它在运行时消耗了整个CPU核心;CPU消耗的负责调用是boost::进程间::进程间信号量::定时等待

下面是重现这种行为的代码部分

#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;
        }
    }
}
#包括
#包括
#包括
#包括
#包括
静态bool gStopRequested(错误);
结构ShmObj
{
boost::进程间::进程间_信号量mSemaphore;
ShmObj():mSemaphore(0){};
~ShmObj(){};
};
int main(字符*argc,常量字符**argv)
{
boost::进程间::共享内存对象*lShmObj=NULL;
std::string lShmObjName(“My_Boost_进程间测试”);
boost::进程间::映射的_区域*lRegion;
ShmObj*lObj;
//创建共享段
尝试
{
lShmObj=new boost::进程间::共享内存对象(boost::进程间::仅创建,lShmObjName.c_str(),boost::进程间::读写);
}
捕获(boost::进程间::进程间异常&ex)
{
如果(例如:获取错误代码()!=boost::进程间::已存在错误)
{

std::cerr我成功地使用了Mach信号量,而不是boost::interprocess的信号量……请参见

这可能会解决一些问题:
#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;
        }
    }
}