Boost logo

Boost :

From: Yitzhak Sapir (yitzhaks_at_[hidden])
Date: 2003-11-26 08:22:34


> -----Original Message-----
> From: Peter Dimov
> Sent: Monday, November 17, 2003 4:49 PM
> To: Boost mailing list
> Subject: Re: [boost] Re: problem with shared_ptr
>
> > Try
> >
> > http://www.pdimov.com/cpp/shared_count_x86_exp2.hpp
> >
> > then.
>
> ... and please report the results. It would also help if you
> can try the
> same test with #define BOOST_LWM_USE_CRITICAL_SECTION.
>
> Thanks in advance.

Hi there.

I had run the following program to test the behavior of shared count, on a 2 cpu system with hyperthreading, 3 times with each different implementation.
The original code took 1.593 seconds on average
The new code in the link above took 1.031 seconds on average
Using the define (ie, the win32 api) took 43.348 seconds on average
Using the define but modifying the code to contain an InitializeCriticalSectionAndSpinCount took the following amounts of time:
        spinlock=4,000 took 42.901 seconds on average
        spinlock=100,000 took 39.192 seconds on average
        spinlock=1,000,000 took 9.547 seconds on average
At the task manager, except for the last case, the CPU showed at 33%. But when the spin count was a million, it went to 100%. This test though clearly points to the new code being the best so far. shared_count.hpp is version 1.33 in the cvs which is approximately release 1.30.0.

This does not correspond to other results we have seen where having used the private lightweight mutex to guard certain memory pool accesses gave worse performance than using InitializeCriticalSectionAndSpinCount(..,4000). But those results are more of an empiric nature. Which brings me to:

> > Totally broken "boosted-lightweight" mutex stuff aside for a moment,
>
> Yep. "Totally broken" unfortunately looks like an accurate assessment of the
> situation.

What is totally broken about it? Can you direct me to the discussion of the problem issues with the lightweight mutex? (Based on the empiric results mentioned above we've moved to using a private implementation of a spinlock, that is not portable yet. But we're interested).

Thanks a lot.

The tester code is as follows:
#include "stdafx.h"
// #define BOOST_LWM_USE_CRITICAL_SECTION
#include "system_basic_defs.h"
#include <boost/shared_ptr.hpp>
#include <boost/array.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include "time_recorder_t.h"

boost::shared_ptr<int> x;

void thread_func()
{
        try {
                for (int i = 0; i < 1000000; ++i) {
                        boost::shared_ptr<int> y(x);
                        if (*y != 3) {
                                std::cout << "error!\n";
                        }
                }
        } catch(...) {
                std::cout << "exception!\n";
        }
}

int main(int argc, char* argv[])
{
        try {
                x.reset(new int(3));
                boost::array<boost::thread*, 5> threads;
                int i;

                time_recorder_t tr("thread_funcs");
                for (i = 0; i < threads.static_size; ++i)
                        threads[i] = new boost::thread(&thread_func);

                for (i = 0; i < threads.static_size; ++i)
                        threads[i]->join();

                for (i = 0; i < threads.static_size; ++i)
                        delete threads[i];
        } catch(...) {
                std::cout << "main exception!\n";
        }

        return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk