Crash in multiple threading with shared resource
-
several sockets (multiple threading) on server-side share same public resource, such as user-array, for deleting, adding users etc. sometimes, server crashes. reason of server's crashing can't be debugged properly, but, from debug hints, I guess the reason is that when a socket is doing something on user-array but another socket start adding or deleting users on same array. If this is real reason, how to avoid it? lock user-array? or other ways? Thanks for comments.
-
several sockets (multiple threading) on server-side share same public resource, such as user-array, for deleting, adding users etc. sometimes, server crashes. reason of server's crashing can't be debugged properly, but, from debug hints, I guess the reason is that when a socket is doing something on user-array but another socket start adding or deleting users on same array. If this is real reason, how to avoid it? lock user-array? or other ways? Thanks for comments.
You HAVE to use synchronization if you have multiple threads accessing the same object if that object is being altered!!!!! You can use a critical section[^] or a mutex[^] to ensure only one thread can use the protected object at a time.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
several sockets (multiple threading) on server-side share same public resource, such as user-array, for deleting, adding users etc. sometimes, server crashes. reason of server's crashing can't be debugged properly, but, from debug hints, I guess the reason is that when a socket is doing something on user-array but another socket start adding or deleting users on same array. If this is real reason, how to avoid it? lock user-array? or other ways? Thanks for comments.
Put the code that accesses the shared resources between the
EnterCriticalSection
andLeaveCriticalSection
APIs.«_Superman_» _I love work. It gives me something to do between weekends.
-
several sockets (multiple threading) on server-side share same public resource, such as user-array, for deleting, adding users etc. sometimes, server crashes. reason of server's crashing can't be debugged properly, but, from debug hints, I guess the reason is that when a socket is doing something on user-array but another socket start adding or deleting users on same array. If this is real reason, how to avoid it? lock user-array? or other ways? Thanks for comments.
See also :) :
class CUserInfo;
typedef CUserInfoArray CArray;
class CUserSyncArray : protected CUserInfoArray
{
CCriticalSection m_cLock;public:
CUserSyncArray() {..}
virtual ~CUserSyncArray() {..}// Beginn of all needed CArray methods:
CUserInfo* GetAt(INT_PTR iPos) {
CSingleLock cExceptionFreeLock(&m_cLock, true);
return CUserInfoArray::GetAt(iPos);
}
//..and so on, for all needed methods :)
};They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)
-
several sockets (multiple threading) on server-side share same public resource, such as user-array, for deleting, adding users etc. sometimes, server crashes. reason of server's crashing can't be debugged properly, but, from debug hints, I guess the reason is that when a socket is doing something on user-array but another socket start adding or deleting users on same array. If this is real reason, how to avoid it? lock user-array? or other ways? Thanks for comments.
includeh10 wrote:
reason of server's crashing can't be debugged properly, but, from debug hints, I guess the reason is that when a socket is doing something on user-array but another socket start adding or deleting users on same array.
In continuation with Superman :-) you can also utilize service of Events or semaphore also. the benefits of semaphore ( you can solve reader writer problem using it)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You