写多线程程序,如果需要对基元类型如(int,long等)进行同步,需要处理。直接lock肯定是不支持的?
解决方案 »
- c# winform dataGridView1中当第三列的cell发生变化时触发一个事件
- 简单多线程问题(速度结贴)
- 关于C# API的调用问题
- MDI窗体的问题
- 用c#开发的网站平台,sql数据库,系统用win 2003,100万人同时在线需要多少台服务器?每天100万的独立ip访问量又需要多少台服务器大概?
- 关于listbox的一个问题?急,请解决!
- 1、如何在 C#语言中调用 VC语言的静态程序库和动态程序库。2、如何有效地保护C#语言的源代码不被反编译锥形。3、在哪里可以找到一些关于
- 想让自制C#组件加载时调入XML文件,退出时保存XML文件,怎么办?
- C#winform调用mouse_event必须调用两次右键down才能调出右键菜单
- 邮件合并发送的问题
- C#中多个线程要同时调用form上的窗体控件,如何使这些线程同步。
- [求助]请问怎样把存储在ImageList中的图片转换为Icon?
有关此类型所有成员的列表,请参阅 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.VisualBasicClass 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();
}
}