SquishyOleo
November 4th, 2009, 02:04 PM
Hi All,
I'm having a lot of trouble finding an answer to this - hopefully you can provide some guidance.
I'm currently in the design phase of a project which is multi-threaded and it maintains an array of connected client objects (CClient). Each CClient object is specific to it's related connection storing information such as statistics, positioning, connection details, etc, etc. This list can be accessed by multiple threads at different times (for reading/writing) and I'm trying to come up with a good way to lock the list during thread accesses. At this point in time I've decided that a per-client lock system would be the best way to go.
Now for implementation. There are two ways I see this being done but cannot seem to get either to work. The first would be to implement a "gateway" type of function where all calls to a CClient instance would be made through a single function:
getClient().connection.setAddress(".....");
This "getClient()" function would lock and then unlock the CClient object once "setAddress()" had completed its tasks.
The second way would be to have specific Lock() and Unlock() member functions of each CClient instance. Then when a thread wants to manipulate the CClient it calls "client.Lock()", does what it needs to, then calls "client.Unlock()". The problem with this method is that I can make CMutex a member variable of the CClient class but can't get CSingleLock set up as a member variable since it needs a pointer to the CMutex object when it's constructed.
I think I'd prefer using the first method as it internalizes the locking mechanism to the CClient object itself - I'm just not sure how to implement it. Can anyone point me in the proper direction? Or maybe someone has a better idea on how to handle this?
Thanks!
I'm having a lot of trouble finding an answer to this - hopefully you can provide some guidance.
I'm currently in the design phase of a project which is multi-threaded and it maintains an array of connected client objects (CClient). Each CClient object is specific to it's related connection storing information such as statistics, positioning, connection details, etc, etc. This list can be accessed by multiple threads at different times (for reading/writing) and I'm trying to come up with a good way to lock the list during thread accesses. At this point in time I've decided that a per-client lock system would be the best way to go.
Now for implementation. There are two ways I see this being done but cannot seem to get either to work. The first would be to implement a "gateway" type of function where all calls to a CClient instance would be made through a single function:
getClient().connection.setAddress(".....");
This "getClient()" function would lock and then unlock the CClient object once "setAddress()" had completed its tasks.
The second way would be to have specific Lock() and Unlock() member functions of each CClient instance. Then when a thread wants to manipulate the CClient it calls "client.Lock()", does what it needs to, then calls "client.Unlock()". The problem with this method is that I can make CMutex a member variable of the CClient class but can't get CSingleLock set up as a member variable since it needs a pointer to the CMutex object when it's constructed.
I think I'd prefer using the first method as it internalizes the locking mechanism to the CClient object itself - I'm just not sure how to implement it. Can anyone point me in the proper direction? Or maybe someone has a better idea on how to handle this?
Thanks!