Search code examples
c++macosboostmutexboost-interprocess

Too much CPU consumption by boost scoped_lock


I have some code which waits for write operation on Shared Memory. If no one writes it continues to wait.

    Test* Foo::Get() 
    {
        boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> lock ( mutex ) ; // mutex is boost::interprocess::interprocess_mutex
        if ( this->check == 0 )
            this->interprocessCondition.wait ( lock ) ; // interprocessCondition is boost::interprocess::interprocess_condition

...
    }

When i did sampling, I found out it consumes around 90% of CPU.

Can some one help me fix this performance problem? Please see the attached image.

Screen Shot


Solution

  • boost::interprocess, unfortunately, uses busy-waiting for locks on many platforms (apparently including OSX). You'll want to use a lock native to your platform that actually sleeps.