Boost logo

Boost Users :

From: Delfin Rojas (drojas_at_[hidden])
Date: 2008-03-14 17:41:52


On Tuesday, March 11, 2008 5:05 AM Yasmina Berber wrote:

> Sent:
> To: boost-users_at_[hidden]
> Subject: [Boost-users] [Mutex] behaviour??
>
> Hi, everybody
> I have a problem to understand mutex behaviour in a server client
> program .
> basically there are two threads that execute different code in two
> different classes but manipulate the same resource:
> fisrt Class is Subscriber with these members :
>
> public:
> ACE_Unbounded_Set<client_reference> m_set;
> typedef boost::recursive_mutex Mutex;
> typedef boost::recursive_mutex::scoped_lock Lock;
> Mutex mutex_set;
>
> and methods:
> Subscribe (client_reference) : inserts (add) a reference client
> to the set
> { ...
> Lock theScope( mutex_set);
> m_set.insert (client_reference)
> ...
> }
> Unsubscribe(client_reference) : removes a reference client from the
set
> { ...
> Lock theScope( mutex_set);
> m_set.remove (client_reference)
> }
>
>
> the second class is Manager that has a shared pointer to Subscriber
> (SubscriberPtr) and does a specific opeartion on each
> client_reference inside a for loop in the method
> Manage()
> {
> boost::recursive_mutex::scoped_lock scoped_lock(subscriberPtr-
> >mutex_set)
> for (Subscriber::Set_Iterator iter (subscriberPtr->m_set);
iter.next
> (...) != 0 ; iter.advance ())
> {
> specific opeartion on each client reference
> }
> }
> What happens is that a client can be removed (call to Unsubscribe())
by
> the first thread during the second one is executing manage() : the set
> is not well protected and this causes of course seg fault.
>
> I probably do something wrong,
> If you need more details please let me know.
> Regards.

If your code is exactly as you have it here I see nothing wrong with it.
You are locking the mutex inside Manage so it is not possible for
another thread to enter Unsubscribe. Could it be that some action inside
Manage is the one calling Unsubscribe (i.e. from the same thread)? Could
it be that you are copying your instance of Subscriber somewhere so the
instance of Subscriber inside Manage (subscriberPtr) is not the same as
the instance of subscriber on which Unsubscribe gets called?

If you put together a small sample program showing your problem I'll be
happy to take a look.

-delfin


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