Wednesday, July 30, 2008

Critical Section vs. Mutex

Both critical sections and mutex objects can be used to synchronize code execution in a Windows CE system.  Judicious use of these techniques is necessary to prevent sharing conflicts over system resources through synchronized access.  But why would a programmer choose one method over the other?

 

A critical section is defined as a segment of code that can be run by at most one thread at any instance of time. All code that accesses a shared resource should be placed within a critical section in order to synchronize access to the resource. In effect, mutually exclusive access to the resource is guaranteed if all accesses are guarded by the same critical section.

 

A mutex object is ‘signaled’ when it is not owned by a thread. A thread takes ownership of the synchronizing mutex object prior to accessing a shared resource.  If the resource is already in use by another thread, then access will be blocked. Exclusive access can be guaranteed provided all accesses to the resource are guarded by the same mutex object.

 

Now to highlight the differences: In general, mutex objects are more CPU intensive and slower to use than critical sections due to a larger amount of bookkeeping, and the deep execution path into the kernel taken to acquire a mutex. The equivalent functions for accessing a critical section remain in thread context, and consist of merely checking and incrementing/decrementing lock-count related data.  This makes critical sections light weight, fast, and easy to use for thread synchronization within a process. The primary benefit of using a mutex object is that a mutex can be named and shared across process boundaries.

 

Best practices to optimize for performance include:

  • Minimizing the need to synchronize across process boundaries during software design
  • Minimizing the use of mutex objects whenever possible
  • Using critical sections for synchronization whenever possible

 



More information on critical section and mutex APIs can be found on MSDN:

Friday, July 25, 2008

Microsoft Press: Windows Embedded CE 6.0 Fundamentals

Microsoft Press: Windows Embedded CE 6.0 Fundamentals by Stanislav Pavlov and Pavel Belevsky.

This book is intended for everyone who develops or plans the development of embedded devices based on Windows Embedded CE. If you are just learning about the Windows Embedded CE operating system, this book can serve as a starting point for further learning. If you are already familiar with Windows Embedded CE, this book provides advice and recommendations for application development.

Here's the chapter listing for the book:

  • Chapter 1: Introduction
  • Chapter 2: Operating System and Application
  • Chapter 3: Operating System Architecture
  • Chapter 4: Subsystem for Building an Operating System
  • Chapter 5: Board Support Package (BSP)
  • Chapter 6: Driver Architecture
  • Chapter 7: Loading the Operating System
  • Chapter 8: Building Devices
  • Chapter 9: Application Development