Boost logo

Boost Users :

Subject: Re: [Boost-users] Poor/erratic boost::interprocess named_semaphore performance
From: Davidson, Josh (josh.davidson_at_[hidden])
Date: 2012-02-12 23:59:56


Ok, so I converted my example to native Windows semaphores, and it consistently completes in less than 250ms (up to 160x faster). Something is definitely going on.

-----Original Message-----
From: Davidson, Josh
Sent: Sunday, February 12, 2012 9:01 PM
To: boost-users_at_[hidden]
Subject: Poor/erratic boost::interprocess named_semaphore performance

I'm experiencing performance issues using named semaphores on Windows 7 x64. Currently, I'm on 1.49 beta 1, but the behavior was similar on 1.48 and 1.47. Below, I'm copying two sample programs. They simply synchronize with one another using a pair of semaphores. To run them, start the first program and then the second. They will synchronize with each other 100k times and then the second program will spit out the elapsed time. You can restart the second program to run again without bringing down the first program.

On Windows, the elapsed time the first time around is anywhere from 20 - 40 seconds. If you leave the second program running, the following run will be only about 2-3 seconds. A third run it will be back up to 20-40 seconds, and it will keep bouncing back and forth like that. On Linux, the total elapsed time is consistently less than half a second.

I'm guessing the Windows implementation isn't as efficient, but I wouldn't expect it to be 100 times slower than Linux. The other perplexing thing is how wildly different the results are on Windows from run to run.

=====================Test1.cpp==========================================
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;
#include <boost/interprocess/sync/named_semaphore.hpp>
using namespace boost::interprocess;
#include <iostream>
using namespace std;

int main() {
        named_semaphore::remove("sem1");
        named_semaphore::remove("sem2");
                        
        named_semaphore sem1(create_only_t(), "sem1", 0);
        named_semaphore sem2(create_only_t(), "sem2", 0);
        
        while(true) {
                sem1.wait();
                sem2.post();
        }
        
        return 0;
}

=====================Test2.cpp==========================================
#include <boost/interprocess/sync/named_semaphore.hpp>
using namespace boost::interprocess;

#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;
#include <iostream>
using namespace std;

int main() {

        const size_t iterations = 100000;
        named_semaphore sem1(open_only_t(), "sem1");
        named_semaphore sem2(open_only_t(), "sem2");
        
        ptime start = boost::posix_time::microsec_clock::local_time();
        for(size_t i = 0; i < iterations; ++i) {
                sem1.post();
                sem2.wait();
        }

        ptime end = boost::posix_time::microsec_clock::local_time();
        time_duration delta = end - start;
        
        double seconds = double(delta.total_nanoseconds())/1000000000.0;

        cout << "Total elapsed time: " << seconds << endl;;
        cout << "Time per iteration: " << (seconds/double(iterations)) << endl;
        return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net