Boost logo

Boost :

From: Jason Hise (chaos_at_[hidden])
Date: 2005-05-05 20:16:15


Jonathan Turkanis wrote:

>It only takes a few minutes to figure out how to use Boost.Threads. I suggest
>you implement and test a lock policy using Boost.Threads and make it available
>to reviewers.
>
Alright, I have completed a sample program, which is attached. The
example has been added to the download package as well. Hopefully this
alleviates some concerns.

Tom Brinkman wrote:

>Was the "singleton" library tested on GCC.
>I'm getting many compiler errors on 3.34.
>
>
The newly uploaded package should fix most of those errors, please let
me know if any still need to be corrected.

-Jason


#include <iostream>
#include <boost/singleton.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include <boost/thread/thread.hpp>

using namespace boost;
using namespace boost::singleton;

// singleton to hold the mutex
class mutex_holder : public basic_singleton < mutex_holder >
{
private:
    // make sure singleton is created before main
    // (assumes no secondary threads are created until main)
    static mutex_holder self;

    // the mutex to lock (must be recursive with
    // current singleton implementation)
    recursive_mutex mutex_;

public:
    recursive_mutex & get_mutex ( )
    {
        return mutex_;
    }
};

// our custom locking policy
class shared_singleton_lock : recursive_mutex::scoped_lock
{
public:
    shared_singleton_lock ( ) :
      recursive_mutex::scoped_lock ( mutex_holder::pointer ( )->get_mutex ( ) )
    {
    }

    // the following are needed because locks need to be able to be copied

    shared_singleton_lock ( const shared_singleton_lock & rhs ) :
    recursive_mutex::scoped_lock ( mutex_holder::pointer ( )->get_mutex ( ) )
    {
    }

    shared_singleton_lock & operator = ( const shared_singleton_lock & rhs )
    {
    }
};

// singleton which uses the locking policy
class shared_singleton :
    public singleton_ex
    <
        shared_singleton,
        lifo_lifetime_ex
        <
            instant_creation,
            create_statically,
            shared_singleton_lock
>
>
{
private:
    // depends upon mutex_holder
    mutex_holder::pointer holder_ptr;
    int val;

protected:
    shared_singleton ( ) : val ( 0 )
    {
    }

public:
    // Put something in here designed to fail if two
    // threads access the function at the same time
    void DoSomething ( )
    {
        std::cout << "Entering function, val == " << val << "\n";
        val = val + 1;
        std::cout << "Exiting function, val == " << val << "\n";
    }
};

void test ( )
{
    shared_singleton::pointer ptr;

    // we have 6 threads, so if all goes well
    // the last thing printed should be 6000
    for ( int i = 0; i < 1000; ++i )
    {
        ptr->DoSomething ( );
    }
}

int main ( )
{
    thread t1 ( &test );
    thread t2 ( &test );
    thread t3 ( &test );
    thread t4 ( &test );
    thread t5 ( &test );
    thread t6 ( &test );

    // must join all threads so that program does not end
    // while threads are executing ( this would kill the
    // singleton instances and probably result in a crash )
    t1.join ( );
    t2.join ( );
    t3.join ( );
    t4.join ( );
    t5.join ( );
    t6.join ( );
    return 0;
}


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