Boost logo

Boost Users :

From: Jonas Hansson (jonas_at_[hidden])
Date: 2007-11-06 10:26:07


On Tue, 06 Nov 2007 13:19:44 +0100, Honki <nagut_at_[hidden]> wrote:

> Hi boost-users,
>
> I'm new in the mailing list, so I hope, that I'm right here and you can
> help me.
>
> I want to start a ThreadClass that includes some boost::mutex variables,
> but it doesn't work because of the "noncopyable" definition of the
> mutex-class.
>
> *******************MyClass*******************
> ......
> class OutputProcessor_C
> {
> public:
> ClientOutputProcessor_C();
> virtual ~ClientOutputProcessor_C();
>
> void operator()()
> {
> while( true )
> {
> boost::xtime wakeUpTime;
> boost::xtime_get(&wakeUpTime, boost::TIME_UTC);
> wakeUpTime.sec += ( 0.05 );
> boost::thread::sleep( wakeUpTime );
>
> Process();
> }
> }
>
> protected:
> void Process();
> boost::mutex m_publishDataMutex;
> ......
> *********************************************
>
> *******************MyMain********************
> ......
> int main(int argc, char* argv[])
> {
> // Classpointer
> boost::shared_ptr<OutputProcessor_C()> outputProcessor(new
> OutputProcessor_C());
>
> // generate Thread
> boost::shared_ptr<boost::thread> threadOutputProcessor(new
> boost::thread(*outputProcessor));
> ......
> *********************************************
>
> When I tried to compile the source code, there is an error - "It is not
> possible to copy the constructor for the class 'boost::mutex'". Where is
> the problem? I don't want to copy anything. If it is not possible to
> generate a simple thread class that includes some mutexes, that is
> really weak.
>
> Greetings,
> Matthias

In fact you are trying to copy the mutex.
If you read at http://www.boost.org/doc/html/boost/thread.html you will
see the following:
explicit thread(const boost::function0<void>& threadfunc);
   Effects: Starts a new thread of execution and constructs a thread object
representing it. Copies threadfunc (which in turn copies the function
object wrapped by threadfunc) to an internal location which persists for
the lifetime of the new thread of execution. Calls operator() on the copy
of the threadfunc function object in the new thread of execution.

I had the problem, I solved it by letting my mutex be a reference which I
send to it in the constructor.
e.g.

class MyThreadClass
{
public:
        MyThreadClass( boost::mutex& _mutex ) : m_mutex( _mutex )
        void operator()()
        {
                ...
                {
                        boost::mutex::scoped_lock myLock( m_mutex );
                        // do sensitive stuff here
                }
                ...
        }
protected:
        boost::mutex &m_mutex;
};

int main( int argc, char** argv )
{
        boost::thread_group myThreadGroup;

        MyThreadClass *pThreadClass = new MyThreadClass( mutexDb );
        myThreadGroup.create_thread( *pThreadClass );
}

-- 
Jonas Hansson

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