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.

 

 


















本文转自cnn23711151CTO博客,原文链接:http://blog.51cto.com/cnn237111/515758 ,如需转载请自行联系原作者







相关文章
|
7月前
|
C++
C++11/14/17中提供的mutex系列区别
C++11/14/17中提供的mutex系列类型如下:
|
9月前
|
Linux 编译器 C语言
互斥锁mutex
互斥锁mutex
48 0
pthread_mutex_unlock()出错
pthread_mutex_unlock()出错
104 0
使用超时加锁:pthread_mutex_timedlock
使用超时加锁:pthread_mutex_timedlock
152 0
|
Linux API
pthread_mutex_init & 互斥锁pthread_mutex_t的使用
pthread_mutex_init l         头文件: #include l         函数原型: int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr); pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; l         函数作用: 该函数用于C函数的多线程编程中,互斥锁的初始化。
1823 0
|
C++
【C++ 语言】pthread_mutex_t 互斥锁
【C++ 语言】pthread_mutex_t 互斥锁
232 0