Question RE. CriticalSection
-
Hi, I'm sharing an object between 2 threads and I need to synchronize it. The following is a model of my code:
class SynchronizedObject { SynchronizedObject() //constructor { InitializeCriticalSection(&m_ThreadSynch); } CRITICAL_SECTION m_ThreadSynch; void Lock() { ::EnterCriticalSection(&m_ThreadSynch); } void Unlock() { ::LeaveCriticalSection(&m_ThreadSynch); } //now the functions with problem void A() { Lock(); B(); Unlock(); } void B() { Lock(); //do some stuff Unlock(); } };
For some reason when I call A() my code doesn't lock up. Am I using the Critical Section incorrectly? Or does it check which thread is trying to enter the critical section and allows the same thread to enter it multiple times (which I thought wasn't the case). What about Mutexes? Aside from synchronizing accross processes do they behave differently? What about Semaphores? Note: I've also tried to just put a member object CCriticalSection synch; and call synch.Lock()/Unlock() where appropriate. Same result. -
Hi, I'm sharing an object between 2 threads and I need to synchronize it. The following is a model of my code:
class SynchronizedObject { SynchronizedObject() //constructor { InitializeCriticalSection(&m_ThreadSynch); } CRITICAL_SECTION m_ThreadSynch; void Lock() { ::EnterCriticalSection(&m_ThreadSynch); } void Unlock() { ::LeaveCriticalSection(&m_ThreadSynch); } //now the functions with problem void A() { Lock(); B(); Unlock(); } void B() { Lock(); //do some stuff Unlock(); } };
For some reason when I call A() my code doesn't lock up. Am I using the Critical Section incorrectly? Or does it check which thread is trying to enter the critical section and allows the same thread to enter it multiple times (which I thought wasn't the case). What about Mutexes? Aside from synchronizing accross processes do they behave differently? What about Semaphores? Note: I've also tried to just put a member object CCriticalSection synch; and call synch.Lock()/Unlock() where appropriate. Same result.Critical sections block separate threads from entering the critical section, not the same thread. None of the synchronization objects, near as I ever read, will block the same thread from entering them. Mutexes behave differently because they can be 'abandoned'. If the process owning a mutex exits before releasing the mutex, another process waiting for it will be rleased with the result WAIT_ABANDONED. Semaphores are for controlling 'metered access' to an object. Now, in your instance, if Thread 2 called B(), then Thread1 would not be able to enter A() until Thread2 was done processing B(). Likewise, if Thread1 entered A() first, then thread2 could not enter A() or B() until Thread1 was done with A().
-
Hi, I'm sharing an object between 2 threads and I need to synchronize it. The following is a model of my code:
class SynchronizedObject { SynchronizedObject() //constructor { InitializeCriticalSection(&m_ThreadSynch); } CRITICAL_SECTION m_ThreadSynch; void Lock() { ::EnterCriticalSection(&m_ThreadSynch); } void Unlock() { ::LeaveCriticalSection(&m_ThreadSynch); } //now the functions with problem void A() { Lock(); B(); Unlock(); } void B() { Lock(); //do some stuff Unlock(); } };
For some reason when I call A() my code doesn't lock up. Am I using the Critical Section incorrectly? Or does it check which thread is trying to enter the critical section and allows the same thread to enter it multiple times (which I thought wasn't the case). What about Mutexes? Aside from synchronizing accross processes do they behave differently? What about Semaphores? Note: I've also tried to just put a member object CCriticalSection synch; and call synch.Lock()/Unlock() where appropriate. Same result.Both CriticalSection and Mutex are recursive in Windows. Recursive mutex will not let the same thread block itself. You could try using the boost thread library, which does have non-recursive mutex. If you don't mind sticking with recursive logic, I recommend you use the smart pointers in the following link, which act as synchronized wrapper class for your object. http://code.axter.com/sync_ptr.h http://code.axter.com/sync_ctrl.h Top ten member of C++ Expert Exchange. http://www.experts-exchange.com/Cplusplus
-
Hi, I'm sharing an object between 2 threads and I need to synchronize it. The following is a model of my code:
class SynchronizedObject { SynchronizedObject() //constructor { InitializeCriticalSection(&m_ThreadSynch); } CRITICAL_SECTION m_ThreadSynch; void Lock() { ::EnterCriticalSection(&m_ThreadSynch); } void Unlock() { ::LeaveCriticalSection(&m_ThreadSynch); } //now the functions with problem void A() { Lock(); B(); Unlock(); } void B() { Lock(); //do some stuff Unlock(); } };
For some reason when I call A() my code doesn't lock up. Am I using the Critical Section incorrectly? Or does it check which thread is trying to enter the critical section and allows the same thread to enter it multiple times (which I thought wasn't the case). What about Mutexes? Aside from synchronizing accross processes do they behave differently? What about Semaphores? Note: I've also tried to just put a member object CCriticalSection synch; and call synch.Lock()/Unlock() where appropriate. Same result. -
Hi, I'm sharing an object between 2 threads and I need to synchronize it. The following is a model of my code:
class SynchronizedObject { SynchronizedObject() //constructor { InitializeCriticalSection(&m_ThreadSynch); } CRITICAL_SECTION m_ThreadSynch; void Lock() { ::EnterCriticalSection(&m_ThreadSynch); } void Unlock() { ::LeaveCriticalSection(&m_ThreadSynch); } //now the functions with problem void A() { Lock(); B(); Unlock(); } void B() { Lock(); //do some stuff Unlock(); } };
For some reason when I call A() my code doesn't lock up. Am I using the Critical Section incorrectly? Or does it check which thread is trying to enter the critical section and allows the same thread to enter it multiple times (which I thought wasn't the case). What about Mutexes? Aside from synchronizing accross processes do they behave differently? What about Semaphores? Note: I've also tried to just put a member object CCriticalSection synch; and call synch.Lock()/Unlock() where appropriate. Same result.