Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A mutex can lock across multiple processes. This would be useful in Windows Phone if you have a scheduled task running that needs exclusive access to a resource. In order to lock a mutex across processes the Mutex must be given a name.</p> <p>A monitor can lock only within a process.</p> <p>Mutex Example:</p> <p>Phone App Task:</p> <pre><code> public class DatabaseService { private Mutex _mut=new Mutex("mutex control",false); public void AddToDatabase(DbObject row) { mut.WaitOne(); SaveRow(row); mut.ReleaseMutex(); } } </code></pre> <p>Scheduled Task class:</p> <pre><code> public class ResourceUtilisation { private Mutex _mut=new Mutex("mutex control",true); //.. does stuff private static void UseResource() { // Wait until it is safe to enter. _mut.WaitOne(); //Go get dataabse and add some rows DoStuff(); // Release the Mutex. _mut.ReleaseMutex(); } } </code></pre> <p>In the above example we're only allowing one app at a time access to the local database resource. This is why we'd use a Mutex.</p> <p>Monitor Example (using lock syntax):</p> <p>Phone App Task:</p> <pre><code> public class DatabaseService { private object _locker=new object(); public void AddToDatabase(DbObject row) { lock(_locker) SaveRow(row); } } </code></pre> <p>Scheduled Task class:</p> <pre><code> public class ResourceUtilisation { private object _locker=new object(); //.. does stuff private static void UseResource() { //Go get dataabse and add some rows lock(_locker) DoStuff(); } } </code></pre> <p>In this example we can stop more than one application thread entering SaveRow and we can stop more than one ScheduledTask thread from entering the DoStuff method. What we can't do with a Monitor is ensure that only one thread is accessing the local DB at once.</p> <p>That's basically the difference. Monitor is much faster than a Mutex as well.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload