Boost logo

Boost Users :

From: Yasmina Berber (yasmina.berber_at_[hidden])
Date: 2008-03-11 08:05:23


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.


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