Thread synchronization problems
-
Hi All, I am beginer for Threading program.I did some programs on all the four thread synchronization classes(CCriticalSection,CMutex,CSemaphore, CEvent). Now my question is "What happens if a thread which locks a resource crashes/terminates without unlocking it?" Thanks in Advance.
-
Hi All, I am beginer for Threading program.I did some programs on all the four thread synchronization classes(CCriticalSection,CMutex,CSemaphore, CEvent). Now my question is "What happens if a thread which locks a resource crashes/terminates without unlocking it?" Thanks in Advance.
ss431 wrote:
"What happens if a thread which locks a resource crashes/terminates without unlocking it?"
That resource will be locked for ever and no other threads can lock it again. Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
ss431 wrote:
"What happens if a thread which locks a resource crashes/terminates without unlocking it?"
That resource will be locked for ever and no other threads can lock it again. Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
Thank you for your reply Raj, If the resource is locked what we have to do to unlock that locked resource and make it used by other Threads. If the resource is locked by CMutex object then there is no problem as it will automatically unlocks the locked resource after the thread whick locks it is exited. But what about the situation if we use CCriticalSetion or CSemaphore,as they don't release the resources automatically.
-
Hi All, I am beginer for Threading program.I did some programs on all the four thread synchronization classes(CCriticalSection,CMutex,CSemaphore, CEvent). Now my question is "What happens if a thread which locks a resource crashes/terminates without unlocking it?" Thanks in Advance.
ss431 wrote:
Now my question is "What happens if a thread which locks a resource crashes/terminates without unlocking it?"
Well, it depends what kind of resource it is and what kind of synchronization object you've used to control the access to the resource. If the resource resides only within your process, you should use a critical section (also known as a fast mutex) to synchronize access to the resource from different threads in the process. If the process crashes, you don't have a problem since the resource is gone as well. A critical section is not a kernel object and is used to synchronize resources between threads in the same process. If it's a global resource you cannot use a critical section to control access to it, you have to use a kernel object such as a mutex and give it a name (identity) in order for other processes to use the same mutex when accessing the same resource. If a process that has locked the global resource with a mutex crashes, the mutex remains locked, preventing other processes from locking it and obviously preventing other processes from using the global resource. This scenario will likely freeze your computer. Mutexes, semaphores and events are kernel objects and can be used for synchronization of access to global resources between processes. Critical sections are not kernel objects and can only be used within a single process to synchronize access to process local resources such as queues shared between threads. Reading tips: To avoid common pitfalls when starting out with multithreaded programming, read this excellent article[^]. To know how to distinguish between when synchronization can or should be avoided and when it's absolutely necessary, read this article[^]. To know how to use synchronization objects, read this article[^].
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
Hi All, I am beginer for Threading program.I did some programs on all the four thread synchronization classes(CCriticalSection,CMutex,CSemaphore, CEvent). Now my question is "What happens if a thread which locks a resource crashes/terminates without unlocking it?" Thanks in Advance.
In addition to Roger's reply... You should also do everything you can to make sure your threads can't crash... Handle exceptions, use good error checking, etc :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: