
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Synchronizing Multiple Processes with the Mutex
|
1045
Discussion
Semaphores are a new piece of the Framework in 2.0 and a welcome one. They are
primarily used for resource counting and are available cross-process when named (as
they are based on the underlying kernel semaphore object). Cross-process may not
sound too exciting to many .NET developers until they realize that cross-process also
means cross-AppDomain. If you are creating additional AppDomains to perform
work in, say, for instance, to hold assemblies you are loading dynamically that you
don’t want to stick around for the whole life of your main AppDomain, the sema-
phore can help you keep track of how many are loaded at a time. Being able to con-
trol access up to a certain number of users can be useful in many scenarios (socket
programming, custom thread pools, etc.).
See Also
See the “Semaphore,” “ManualResetEvent,” and “ParameterizedThreadStart” topics
in the MSDN documentation.
18.11 Synchronizing Multiple Processes with the Mutex
Problem
You have two processes or AppDomains that are running code with actions that you
need to coordinate.
Solution
Use a named Mutex as a common signaling mechanism to do the coordination. A
named
Mutex can be accessed from both pieces of code even when running in differ-
ent processes or AppDomains. ...