在多线程中,为了使数据保持一致性必须要对数据或是访问数据的函数加锁,在数据库中这是很常见的,但是在程序中由于大部分都是单线程的程序,所以没有加锁 的必要,但是在多线程中,为了保持数据的同步,一定要加锁,好在Framework中已经为我们提供了三个加锁的机制,分别是Monitor类、Lock 关键字和Mutex类。
| using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace MonitorLockMutex { class Program { #region variable Thread thread1 = null; Thread thread2 = null; Mutex mutex = null; #endregion static void Main(string[] args) { Program p = new Program(); p.RunThread(); Console.ReadLine(); } public Program() { mutex = new Mutex(); thread1 = new Thread(new ThreadStart(thread1Func)); thread2 = new Thread(new ThreadStart(thread2Func)); } public void RunThread() { thread1.Start(); thread2.Start(); } private void thread1Func() { for (int count = 0; count < 10; count++) { TestFunc("Thread1 have run " + count.ToString() + " times"); Thread.Sleep(30); } } private void thread2Func() { for (int count = 0; count < 10; count++) { TestFunc("Thread2 have run " + count.ToString() + " times"); Thread.Sleep(100); } } private void TestFunc(string str) { Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString()); Thread.Sleep(50); } } } |
可以看出如果不加锁的话,这两个线程基本上是按照各自的时间间隔+TestFunc的执行时间(50mm)对TestFunc函数进行读取。因为线程在开 始时需要分配内存,所以第0次的调用不准确,从第1~9次的调用可以看出,thread1的执行间隔约是80mm,thread2的执行间隔约是 150mm。
| private void TestFunc(string str) { lock (this) { Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString()); Thread.Sleep(50); } } |
| private void TestFunc(string str) { Monitor.Enter(this); Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString()); Thread.Sleep(50); Monitor.Exit(this); } |
让我们分析一下结果,同样从第1次开始。相同线程间的调用时间间隔为线程执行时间+TestFunc调用时间,不同线程间的调用时间间隔为 TestFunc调用时间。例如:连续两次调用thread1之间的时间间隔约为30+50=80;连续两次调用thread2之间的时间间隔约为 100+50=150mm。调用thread1和thread2之间的时间间隔为50mm。因为TestFunc被lock住了,所以一个thread调 用TestFunc后,当其它的线程也同时调用TestFunc时,后来的线程即进被排到等待队列中等待,直到拥有访问权的线程释放这个资源为止。
| private void thread1Func() { for (int count = 0; count < 10; count++) { mutex.WaitOne(); TestFunc("Thread1 have run " + count.ToString() + " times"); mutex.ReleaseMutex(); } } private void thread2Func() { for (int count = 0; count < 10; count++) { mutex.WaitOne(); TestFunc("Thread2 have run " + count.ToString() + " times"); mutex.ReleaseMutex(); } } private void TestFunc(string str) { Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString()); Thread.Sleep(50); } |
可以看出,Mutex只能互斥线程间的调用,但是不能互斥本线程的重复调用,即thread1中waitOne()只对thread2中的
waitOne()起到互斥的作用,但是thread1并不受本wainOne()的影响,可以调用多次,只是在调用结束后调用相同次数的
ReleaseMutex()就可以了。
private void thread1Func() private void thread2Func() }
}
{
for (int count = 0; count < 10; count++)
{
lock (this)
{
mutex.WaitOne();
TestFunc("Thread1 have run " + count.ToString() + " times");
mutex.ReleaseMutex();
}
}
}
{
for (int count = 0; count < 10; count++)
{
lock (this)
{
mutex.WaitOne();
TestFunc("Thread2 have run " + count.ToString() + " times");
mutex.ReleaseMutex()
本文通过实例详细介绍了多线程编程中的同步机制,包括Lock、Monitor及Mutex的使用方法及其区别。通过具体代码展示了如何利用这些机制确保数据的一致性和线程间的正确调度。
1243

被折叠的 条评论
为什么被折叠?



