Subj : Re: Freeing data structures in a threaded application To : comp.programming.threads From : Joe Seigh Date : Wed Feb 16 2005 04:26 pm On 16 Feb 2005 06:29:45 -0800, Luke Ehresman wrote: > Hello, > > I have a program with several threads. A session is created for a user > in one thread and stays around for 600 seconds and will then time out > with inactivity (assuming the user doesn't use the session in 600 > seconds). At that time, one of my threads will free up the session. > > The potential problem I have noticed is that I have another thread that > does stuff to the session in the background. I was thinking this > morning, what happens if the session is freed up while the cache > manager thread is working on that same session? I do have a mutex to > lock the session, but that mutex is in the session data structure > itself, so once the structure is freed, the mutex goes away. > > The only way I can figure to get around this is to have a mutex outside > of the session data structure. But then how do I go about freeing up > that mutex after the session is invalid? It seems I would run into the > same problem. I can't keep around global mutexes because there may > potentially be thousands of users using this, logging in and out all > the time. (i.e. it needs to be scalable) > You can use reference counting to protect the session data. You only free the session data when the last reference is dropped. -- Joe Seigh .