Mutex

 

Mutex,类似同步锁。

通过waitone方法来判断是否有信号来中断阻塞。

Mutex初始化的时候可以指定name。整个系统只有唯一的指定name的mutex。可以通过Mutex.OpenExisting方法来打开指定名字的mutex。

一个简单的示例:

static Mutex m = new Mutex(false);

        static void Main(string[] args)

        {

            Thread t1 = new Thread(thread1);

            Thread t2 = new Thread(thread2);

            t1.Start();

            t2.Start();

        }


        static void thread1()

        {

            Console.WriteLine("thread1");

            m.WaitOne();

            for (int i = 0; i < 100; i++)

            Console.Write(i);

            Console.WriteLine("waiting for 5s");

            Thread.Sleep(5000);

            m.ReleaseMutex();

            Console.WriteLine();

        }

        static void thread2()

        {

            Console.WriteLine("thread2");

            m.WaitOne();

            for (int i = 0; i > -100; i--)

                Console.Write(i);

            m.ReleaseMutex();

            Console.WriteLine();

        }

  http://www.cnblogs.com/city22/archive/2007/02/02/638260.html

http://www.cnblogs.com/hsrzyn/articles/1588140.html

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

A Mutex is like a C# lock, but it can work across multiple processes. In other words, Mutex can be computer-wide as well as application-wide.

Acquiring and releasing an uncontended Mutex takes a few microseconds—about 50 times slower than a lock.

With a Mutex class, you call the WaitOne method to lock and ReleaseMutex to unlock. Closing or disposing a Mutex automatically releases it. Just as with the lock statement, a Mutex can be released only from the same thread that
obtained it.

Mutex是调用的Win32 的API

HANDLE CreateMutex(

   LPSECURITY_ATTRIBUTES lpMutexAttributes,

   BOOL bInitialOwner,

   LPCTSTR lpName

);

这就是他为什么能跨进程访问的原因,正是由于它使用P/Invoke,他的效率问题就凸现出来,明显不如Monitor之类的快,用的时候还需多多斟酌。

 

 A mutex is a mutually exclusive synchronization object. This means it can be acquired by

one and only one thread at a time. The mutex is designed for those situations in which a

shared resource can be used by only one thread at a time. For example, imagine a log file

that is shared by several processes, but only one process can write to that file at any one

time. A mutex is the perfect synchronization device to handle this situation.

The mutex is supported by the System.Threading.Mutex class. It has several constructors. Two commonly used ones are shown here:

public Mutex( )

public Mutex(bool initiallyOwned)

The first version creates a mutex that is initially unowned. In the second version, if

initiallyOwned is true, the initial state of the mutex is owned by the calling thread.

Otherwise, it is unowned.

To acquire the mutex, your code will call WaitOne( ) on the mutex. This method

is inherited by Mutex from the Thread.WaitHandle class. Here is its simplest form:

public bool WaitOne( );

It waits until the mutex on which it is called can be acquired. Thus, it blocks execution of the

calling thread until the specified mutex is available. It always returns true.

When your code no longer needs ownership of the mutex, it releases it by calling

ReleaseMutex( ), shown here:

public void ReleaseMutex( )

This releases the mutex on which it is called, enabling the mutex to be acquired by another

thread.

  

Mutex myMtx = new Mutex();

// ...

myMtx.WaitOne(); // wait to acquire the mutex

// Access the shared resource.

myMtx.ReleaseMutex(); // release the mutex

 

 When the call to WaitOne( ) takes place, execution of the thread will suspend until the mutex can be acquired. When the call to ReleaseMutex( ) takes place, the mutex is released and another thread can acquire it. Using this approach, access to a shared resource can be limited to one thread at a time.

 

The mutex created by the previous example is known only to the process that creates it.However, it is possible to create a mutex that is known systemwide. To do so, you mustcreate a named mutex, using one of these constructors:

public Mutex(bool initiallyOwned, string name)

public Mutex(bool initiallyOwned, string name, out bool createdNew)

In both forms, the name of the mutex is passed in name. In the first form, if initiallyOwned is true, then ownership of the mutex is requested. However, because a systemwide mutex might already be owned by another process, it is better to specify false for this parameter. 

In the second form, on return createdNew will be true if ownership was requested and acquired. It will be false if ownership was denied. (There is also a third form of the Mutex constructor that allows you to specify a MutexSecurity object, which controls access.) Using a named mutex enables you to manage interprocess synchronization.

One other point: It is legal for a thread that has acquired a mutex to make one or more additional calls to WaitOne( ) prior to calling ReleaseMutex( ), and these additional calls will succeed. That is, redundant calls to WaitOne( ) will not block a thread that already owns the mutex. However, the number of calls to WaitOne( ) must be balanced by the same number of calls to ReleaseMutex( ) before the mutex is released.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值