• Mutex 类


    同步基元也可用于进程间同步。

    有关此类型所有成员的列表,请参阅 Mutex 成员

    System.Object
       System.MarshalByRefObject
          System.Threading.WaitHandle
             System.Threading.Mutex

    [Visual Basic]
    NotInheritable Public Class Mutex
    Inherits WaitHandle
    [C#]
    public sealed class Mutex : WaitHandle
    [C++]
    public __gc __sealed class Mutex : public WaitHandle
    [JScript]
    public class Mutex extends WaitHandle

    线程安全

    该类型对于多线程操作是安全的。

    备注

    当两个或更多线程需要同时访问一个共享资源时,系统需要使用同步机制来确保一次只有一个线程使用该资源。Mutex 是同步基元,它只向一个线程授予对共享资源的独占访问权。如果一个线程获取了互斥体,则要获取该互斥体的第二个线程将被挂起,直到第一个线程释放该互斥体。

    可以使用 WaitHandle.WaitOne 请求互斥体的所属权。拥有互斥体的线程可以在对 Wait 的重复调用中请求相同的互斥体而不会阻塞其执行。但线程必须调用 ReleaseMutex 方法同样多的次数以释放互斥体的所属权。如果线程在拥有互斥体期间正常终止,则互斥体状态设置为终止,并且下一个等待线程获得所属权。如果没有线程拥有互斥体,则互斥体状态为终止。

    示例

    [Visual Basic]
    ' This example shows how a Mutex is used to synchronize access
    ' to a protected resource. Unlike Monitor, Mutex can be used with
    ' WaitHandle.WaitAll and WaitAny, and can be passed across
    ' AppDomain boundaries.
    Imports System
    Imports System.Threading
    Imports Microsoft.VisualBasic
    Class Test
    ' Create a new Mutex. The creating thread does not own the
    ' Mutex.
    Private Shared mut As New Mutex()
    Private Const numIterations As Integer = 1
    Private Const numThreads As Integer = 3
    Shared Sub Main()
    ' Create the threads that will use the protected resource.
    Dim i As Integer
    For i = 1 To numThreads
    Dim myThread As New Thread(AddressOf MyThreadProc)
    myThread.Name = [String].Format("Thread{0}", i)
    myThread.Start()
    Next i
    ' The main thread exits, but the application continues to
    ' run until all foreground threads have exited.
    End Sub 'Main
    Private Shared Sub MyThreadProc()
    Dim i As Integer
    For i = 1 To numIterations
    UseResource()
    Next i
    End Sub 'MyThreadProc
    ' This method represents a resource that must be synchronized
    ' so that only one thread at a time can enter.
    Private Shared Sub UseResource()
    ' Wait until it is safe to enter.
    mut.WaitOne()
    Console.WriteLine("{0} has entered protected area", _
    Thread.CurrentThread.Name)
    ' Place code to access non-reentrant resources here.
    ' Simulate some work
    Thread.Sleep(500)
    Console.WriteLine("{0} is leaving protected area" & vbCrLf, _
    Thread.CurrentThread.Name)
    ' Release Mutex.
    mut.ReleaseMutex()
    End Sub 'UseResource
    End Class 'MyMainClass
    [C#]
    // This example shows how a Mutex is used to synchronize access
    // to a protected resource. Unlike Monitor, Mutex can be used with
    // WaitHandle.WaitAll and WaitAny, and can be passed across
    // AppDomain boundaries.
    using System;
    using System.Threading;
    class Test
    {
    // Create a new Mutex. The creating thread does not own the
    // Mutex.
    private static Mutex mut = new Mutex();
    private const int numIterations = 1;
    private const int numThreads = 3;
    static void Main()
    {
    // Create the threads that will use the protected resource.
    for(int i = 0; i < numThreads; i++)
    {
    Thread myThread = new Thread(new ThreadStart(MyThreadProc));
    myThread.Name = String.Format("Thread{0}", i + 1);
    myThread.Start();
    }
    // The main thread exits, but the application continues to
    // run until all foreground threads have exited.
    }
    private static void MyThreadProc()
    {
    for(int i = 0; i < numIterations; i++)
    {
    UseResource();
    }
    }
    // This method represents a resource that must be synchronized
    // so that only one thread at a time can enter.
    private static void UseResource()
    {
    // Wait until it is safe to enter.
    mut.WaitOne();
    Console.WriteLine("{0} has entered the protected area",
    Thread.CurrentThread.Name);
    // Place code to access non-reentrant resources here.
    // Simulate some work.
    Thread.Sleep(500);
    Console.WriteLine("{0} is leaving the protected area\r\n",
    Thread.CurrentThread.Name);
    // Release the Mutex.
    mut.ReleaseMutex();
    }
    }
    [C++]
    // This example shows how a Mutex is used to synchronize access
    // to a protected resource. Unlike Monitor, Mutex can be used with
    // WaitHandle.WaitAll and WaitAny, and can be passed across
    // AppDomain boundaries.
    #using <mscorlib.dll>
    using namespace System;
    using namespace System::Threading;
    const int numIterations = 1;
    const int numThreads = 3;
    __gc class Test
    {
    public:
    // Create a new Mutex. The creating thread does not own the
    // Mutex.
    static Mutex* mut = new Mutex();
    static void MyThreadProc()
    {
    for (int i = 0; i < numIterations; i++)
    {
    UseResource();
    }
    }
    private:
    // This method represents a resource that must be synchronized
    // so that only one thread at a time can enter.
    static void UseResource()
    {
    //Wait until it is OK to enter.
    mut->WaitOne();
    Console::WriteLine(S"{0} has entered protected the area",
    Thread::CurrentThread->Name
    );
    // Place code to access non-reentrant resources here.
    // Simulate some work.
    Thread::Sleep(500);
    Console::WriteLine(S"{0} is leaving protected the area\r\n",
    Thread::CurrentThread->Name
    );
    // Release the Mutex.
    mut->ReleaseMutex();
    }
    };
    int main()
    {
    // Create the threads that will use the protected resource.
    for (int i = 0; i < numThreads; i++)
    {
    Thread * myThread = new Thread(
    new ThreadStart(0, Test::MyThreadProc)
    );
    myThread->Name = String::Format(S"Thread {0}", __box(i + 1));
    myThread->Start();
    }
    // The main thread exits, but the application continues to
    // run until all foreground threads have exited.
    }
    

    [JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 语言筛选器

    要求

    命名空间: System.Threading

    平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版

    程序集: Mscorlib (在 Mscorlib.dll 中)

  • 相关阅读:
    (C#)一个WCF简单实例
    C#如何为winform程序打包发布应用(图解教程)
    CSLA之对象状态的跟踪
    猎豹浏览器中附带的2个文件【12306抢票插件相关的】
    将数据库表直接导到Visio中!
    Visual Studio 开发 SharePoint 2010
    Sharepoint 2010 应用范围
    SharePoint自定义权限级别【转】
    SharePoint 2010 安装系统和所需软件
    objectiveC nil,Nil,NULL 和NSNull的小结
  • 原文地址:https://www.cnblogs.com/wzyexf/p/398480.html
Copyright © 2020-2023  润新知