在 WP7 Mango 中实现互斥和监视器

发布于 2024-12-07 23:28:05 字数 98 浏览 0 评论 0原文

互斥体和监视器实现之间有什么区别。任何人都可以帮助我学习 wp7 的这两个内容(wp7 的可实现代码)。请尝试添加一些代码片段,帮助我以简单的方式理解这些技术。 提前感谢 斯特兹马

what is the difference between mutex and monitor implementation. Can any one please help me to learn both of these for wp7(implementable code for wp7). Please try to add some code snippets that help me to understand the techniques in a simple way.
Thanx in advance
Stezma

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有你我更好 2024-12-14 23:28:05

互斥锁可以跨多个进程进行锁定。如果您正在运行需要独占访问资源的计划任务,这在 Windows Phone 中会很有用。为了跨进程锁定互斥锁,必须为互斥锁指定一个名称。

监视器只能在进程内锁定。

互斥示例:

电话应用程序任务:

   public class DatabaseService
    {
    private Mutex _mut=new Mutex("mutex control",false);
    public void AddToDatabase(DbObject row)
    {
        mut.WaitOne();
        SaveRow(row);
        mut.ReleaseMutex();
    }
    }

计划任务类:

 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();
    }
    }

在上面的示例中,我们一次只允许一个应用程序访问本地数据库资源。这就是我们使用互斥锁的原因。

监视器示例(使用锁定语法):

电话应用程序任务:

   public class DatabaseService
    {
    private object _locker=new object();
    public void AddToDatabase(DbObject row)
    {
        lock(_locker)
            SaveRow(row);
    }
    }

计划任务类:

 public class ResourceUtilisation
 {
    private object _locker=new object();
    //.. does stuff
    private static void UseResource()
    {

        //Go get dataabse and add some rows
        lock(_locker)
            DoStuff();
    }
 }

在本示例中,我们可以阻止多个应用程序线程进入 SaveRow,并且可以阻止多个 ScheduledTask 线程进入 DoStuff 方法。我们不能用监视器做的是确保只有一个线程同时访问本地数据库。

这基本上就是区别。监视器也比互斥体快得多。

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.

A monitor can lock only within a process.

Mutex Example:

Phone App Task:

   public class DatabaseService
    {
    private Mutex _mut=new Mutex("mutex control",false);
    public void AddToDatabase(DbObject row)
    {
        mut.WaitOne();
        SaveRow(row);
        mut.ReleaseMutex();
    }
    }

Scheduled Task class:

 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();
    }
    }

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.

Monitor Example (using lock syntax):

Phone App Task:

   public class DatabaseService
    {
    private object _locker=new object();
    public void AddToDatabase(DbObject row)
    {
        lock(_locker)
            SaveRow(row);
    }
    }

Scheduled Task class:

 public class ResourceUtilisation
 {
    private object _locker=new object();
    //.. does stuff
    private static void UseResource()
    {

        //Go get dataabse and add some rows
        lock(_locker)
            DoStuff();
    }
 }

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.

That's basically the difference. Monitor is much faster than a Mutex as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文