NAVmoble - the pocket-sized ERP
Optimized for Microsoft Dynamics NAV and Windows Mobile powered devices

Monday, April 02, 2007

.NET Compact Framework: Mutex vs Critical Section

What do you use for thread synchronization - mutex or critical section?
Check out this blog post from Jeremy Cooke at the Windows CE Base Team Blog

In short:
Using a mutex is more expensive in terms of CPU utilization, but mutexes may be shared outside the processes boundaries.
In .NET CompactFramework terms
Critical section means using System.Threading.Monitor. For mutexes there is a class called System.Threading.Mutex.
The problem here is that there's no easy way to assign a name of the Mutex instance - the constructor with the name argument exists only in the Full .NET version. Using the System.Threading.Mutex class from .NET Compact Framework will prevent you from creating system mutexes - e.g. you can't use the default managed mutex behind the process boundaries.

However there is an implementation from called OpenNETCF.Threading.Mutex2 which may be used to create system-wide named mutexe.

You may see other CF.NET inter-process-communication techniques here as well.

Check this post: SDF 2.2 preview: IPC with EventWaitHandle for yet another IPC method to come in SDF


Jeremy Cooke about Mutex vs Critical Section
System.Threading.Monitor on MSDN
System.Threading.Mutex on MSDN
OpenNETCF.Threading.Mutex2 from
Interprocess Communication with the .NET Compact Framework 1.0

No comments: