Thread: scoped lock and object life time

  1. #1
    Registered User
    Join Date
    Nov 2006

    scoped lock and object life time


    I've created a simple pthread_mutex_lock wrapper to have a scoped lock like in boost::thread

    // .h
    // -----
    #ifndef SCOPEDLOCK_H
    #define SCOPEDLOCK_H
    #include "pthread.h"
    #include <boost/utility.hpp>
    class ScopedLock : boost::noncopyable
    	pthread_mutex_t& mrMutex;
    	ScopedLock(pthread_mutex_t& _m);
    #endif //SCOPEDLOCK_H
    // .cpp
    // -----
    #include "ScopedLock.h"
    ScopedLock::ScopedLock(pthread_mutex_t& _m)
    Now , while I'm trying to re-write some code to make use of this, I'm stucking this this part:

    					ScopedLock sl(mPrioQueueMutex);
    					boost::shared_ptr<SwarmManagerEntity> smanager(new SwarmManagerEntity(
    						entity_id, *this, mrPrioQueue, PrioSet::DEFAULT_PRIO));
    				// put it in the pointer container
    						std::make_pair<int, boost::shared_ptr<SwarmManagerEntity> >(entity_id, smanager));
    					// copy the entity state from swrm to object 
    As you see I'm creating a scope to lock mPrioQueueMutex inside. Then I'm constructing the smanager object, which I also need outside the scope.
    I see 2 possibilities:

    - adding a SwarmManagerEntity default ctor together with an initialize member to do two-step-construction. Then the object could be created outside the scope and initialized inside the critical section.
    Thats work...


    - I could expand the critical section to the point the smanager object isn't needed any more.
    Thats bad...

    Is there another solution?

    Thank you in advance!
    Last edited by pheres; 01-22-2008 at 02:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 33
    Last Post: 05-14-2009, 10:15 AM
  2. read write lock in C#
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-16-2008, 08:49 AM
  3. nested lock?
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 03-23-2008, 08:33 AM
  4. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  5. Threading and mutex's
    By megatron09 in forum C++ Programming
    Replies: 14
    Last Post: 09-07-2006, 02:40 PM