12
votes

There is an existing question regarding difference between Mutex and Critical section but it does not deal with Locks also.

So i want to know whether Critical sections can be used for thread synchronisation between processes.

Also what is meant by signalled states and non-signalled states

4
They are the same thing to me, my guess is that you're using a library which makes some difference between the three (Win32?).Bastien Léonard
Are you saying that Locks like CSingleLock also supports thread synchronisation when it comes to thread across processes. I thought only Mutex supports that. Please correct me if i am wrongckv

4 Answers

8
votes

In Windows critical sections are (mostly) implemented in user mode, and a mutex will switch context to kernel mode (which is slow). If a thread terminates while owning a mutex, the mutex is said to be abandoned. The state of the mutex is set to signaled, and the next waiting thread gets ownership. In the same situation with a critical section all other threads will remain blocked. Critical sections cannot be named so you cannot use them to synchronize several processes.

3
votes

CriticalSections are in process. Named mutexes can be used across processes

Lock is a general term and as such I wouldn't know which platform you mean. For example in C# a lock primitive is a Critical Section.

3
votes

Critical Sections are not kernel objects. They are not identified with any Handle. They can be only used to synchronize the threads belonging to same process. They cannot be used for synchronization across the process.

CSingleLock ( I assume you are referring this as a lock, in this context) is a wrapper class using RAII concept. It helps you to acquire the thread synchronization object (in its constructor) and call Lock and Unlock API in a easy way. ( hiding all internal details of which synchronization objects it is using).

CSingleLock when used with Critical sections, cannot be used across process. Where as Mutex can be used for this purpose.

When a thread acquires Mutex and no other threads can acquire the Mutex then the state of the Mutex is said to be in Non-Signeled state. IF the Mutex is available and no threads have acquired then it is in Signeled state.

2
votes

1) Critical Section is the bridge between User and Interlocked-operations. It uses inetrlocked-exchanged operations to lock and unlock you threads. It works faster than mutexes.

2) Mutex is a kernal object. It works not fast but has some advantages. First of all, named mutexes can be used across processes. Secondly, if the thread is terminated then mutex locked by this thread is unlocked.