Subj : Re: question about the best way to proceed To : comp.programming.threads From : Joe Seigh Date : Wed Mar 02 2005 04:05 pm On 2 Mar 2005 10:59:09 -0800, Luke Ehresman wrote: > I have a question about the best way to solve a problem. I'm hoping to > get some feedback from all of you. > > I have a multi-threaded application that manages sessions for multiple > users. There is a thread in the background that does some processing > on all the sessions constantly. My question is how to best iterate > through the session list in that worker thread. I can't lock the > session list and hold the lock while I do the processing because there > may be a lot of sessions, and to do all the work on all the sessions > would take a long time (and I would hold the lock for a very long > time). > > Let me clarify a bit. I have two mutexes: one for the entire session > list, and one for each individual session. The session list mutex is > used to lock the list when doing operations on the list only. It is > possible to be working on an individual session, but not hold the lock > to the session list. > > Ideally, I would only lock the session I'm currently working with. But > the problem is that I still need to iterate through all the sessions. > When I finish working with that one session, I don't want to have to > start back at the beginning of the session list (I would never get to > the end!). > > Anyone have any thoughts on the best way to solve this? I have some > ideas but I won't present them just yet because I don't want to taint > your responses with my opinions. > Reference counting on the sessions. You can use the global list lock for accessing the reference counts rather than doing interlocked instructions. Reference count is # of links pointing to node plus # threads referencing node. Removing a node is deleting links from list to node but not the links from the node to the next node. When reference count goes to zero, delete link from node to next node. Boost shared_ptr would work if you were doing it in C++. -- Joe Seigh .