Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 919511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:33:27+00:00 2026-05-15T18:33:27+00:00

After porting a code segment from Windows to Mac OS X, I found it

  • 0

After porting a code segment from Windows to Mac OS X, I found it to consume a whole CPU core while running; the responsible call for the CPU consumption is boost::interprocess::interprocess_semaphore::timed_wait.

Here follows the code portion which reproduces this behaviour.

#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;
        }
    }
}

Then, I read that unnamed semaphores were not available under Mac OS X, so I thought it could be because unnamed semaphores were not efficiently emulated… I then tried the following, unsucessfully:

#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;
        }
    }
}

I was actually expecting a better behaviour of boost::interprocess on Mac OS X because of the available Posix primitives, but it is actually not. Any idea for a resolution? Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:33:28+00:00Added an answer on May 15, 2026 at 6:33 pm

    I successfully Used Mach semaphores instead of the ones of boost::interprocess… see http://pkaudio.blogspot.com/2010/05/mac-os-x-no-timed-semaphore-waits.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found a strange issue when porting my code from Visual Studio to gcc.
I'm porting over some templated code from Windows and I'm hitting some compiler differences
I'm in the process of porting some code from Linux to Mac OS X.
I have code that works on Windows, but now that I am porting to
I'm in the process of porting some code from Objective C to C++. I'm
I am porting an application from QT3 to QT4, and keep running into problems
I'm working on porting some ancient code (10.2 era) from NSCoding/plist based archiving to
I'm porting working Clojure code (in a Leiningen project) from 1.2 to 1.3 and
I need to create a porting software that will convert Java code to C#
I am porting over some Java code into Google's Go language and I converting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.