1

在将一段代码从 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 有更好的行为,但实际上并非如此。有解决方案的想法吗?非常感谢。

4

1 回答 1

0

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

于 2010-07-09T16:26:54.757 回答