title: Mac OS X : boost interprocess semaphore timed_wait : Abnormal CPU consumption
body: <p>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.</p>
  
  <p>Here follows the code portion which reproduces this behaviour.</p>
  
  <pre><code>#include &lt;boost/interprocess/sync/interprocess_semaphore.hpp&gt;
  #include &lt;boost/interprocess/shared_memory_object.hpp&gt;
  #include &lt;boost/interprocess/mapped_region.hpp&gt;
  
  #include &lt;boost/thread/thread_time.hpp&gt;
  
  #include &lt;iostream&gt;
  
  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 &amp;ex)
      {
          if (ex.get_error_code() != boost::interprocess::already_exists_error)
          {
              std::cerr &lt;&lt; "Some error" &lt;&lt; std::endl;
              exit(1);
          }
          else
          {
              std::cerr &lt;&lt; "Already exists, just taking it back." &lt;&lt; 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 &amp;ex2)
              {
                  std::cerr &lt;&lt; "D'oh !" &lt;&lt; std::endl;
                  exit(1);
              }
          }
      }
      if (!lShmObj)
      {
          exit(1);
      }
      lShmObj-&gt;truncate(sizeof(ShmObj));
      lRegion = new boost::interprocess::mapped_region(*lShmObj, boost::interprocess::read_write);
      lObj = new (lRegion-&gt;get_address()) ShmObj;
  
      // The loop
      while (!gStopRequested)
      {
          boost::system_time lDeadlineAbsoluteTime = boost::get_system_time() + boost::posix_time::milliseconds(500);
  
          if (lObj-&gt;mSemaphore.timed_wait(lDeadlineAbsoluteTime))
          {
              std::cout &lt;&lt; "acquired !" &lt;&lt; std::endl;
          }
          else
          {
              std::cout &lt;&lt; "tick" &lt;&lt; std::endl;
          }
      }
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>#include &lt;boost/interprocess/sync/named_semaphore.hpp&gt;
  #include &lt;boost/thread/thread_time.hpp&gt;
  
  #include &lt;iostream&gt;
  
  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 &lt;&lt; "acquired !" &lt;&lt; std::endl;
          }
          else
          {
              std::cout &lt;&lt; "tick" &lt;&lt; std::endl;
          }
      }
  }
  </code></pre>
  
  <p>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.</p>