问题描述
我跑到下面预期流动code被锁定在第二次我锁定一个互斥体。两次运行它之后,我意识到它可以锁定很多时候(假定在同一个线程)不停止。如何改变这种行为?
使用系统;
使用System.Collections.Generic;
使用System.Text;
使用的System.Threading;
命名空间测试
{
类节目
{
静态挥发互斥MUT1 =新的mutex();
静态挥发互斥MUT2 =新的mutex();
静态无效的主要(字串[] args)
{
mut1.WaitOne(); Console.WriteLine(1W);
mut2.WaitOne(); Console.WriteLine(2W);
螺纹oThread =新主题(新的ThreadStart(FN2));
oThread.Start();
mut1.WaitOne(); Console.WriteLine(1W);
Console.WriteLine(二);
mut1.ReleaseMutex(); Console.WriteLine(1R);
}
静态无效FN2()
{
Console.WriteLine(第一);
mut2.ReleaseMutex(); Console.WriteLine(2R);
mut1.ReleaseMutex(); Console.WriteLine(1R);
}
}
}
因为一开始我不知道你真的了解互斥,你只能释放他们的背景下(即线程),其中他们被锁,首先,因此,使用它们作为某种后卫分变得毫无意义。
这可能更有意义,使用信号灯在这种情况下。但你还是应该制定出你真正想要做的:)
的使用系统; 使用System.Collections.Generic; 使用System.Text; 使用的System.Threading; 命名空间测试 { 类节目 { 静态旗语SEM1 =新信号量(1,1); 静态旗语sem2 =新信号量(1,1); 静态无效的主要(字串[] args) { sem1.WaitOne(); Console.WriteLine(1W); sem2.WaitOne(); Console.WriteLine(2W); 螺纹oThread =新主题(新的ThreadStart(FN2)); oThread.Start(); sem1.WaitOne(); Console.WriteLine(1W); Console.WriteLine(二); sem1.Release(); Console.WriteLine(1R); } 静态无效FN2() { Console.WriteLine(第一); sem2.Release(); Console.WriteLine(2R); sem1.Release(); Console.WriteLine(1R); } } }
I ran the code below expecting flow to be locked on the 2nd time i lock a mutex. After running it twice i realize it can lock many times (assuming in the same thread) without stopping. How do i change this behavior?
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Test
{
class Program
{
static volatile Mutex mut1 = new Mutex();
static volatile Mutex mut2 = new Mutex();
static void Main(string[] args)
{
mut1.WaitOne(); Console.WriteLine("1W");
mut2.WaitOne(); Console.WriteLine("2W");
Thread oThread = new Thread(new ThreadStart(fn2));
oThread.Start();
mut1.WaitOne(); Console.WriteLine("1W");
Console.WriteLine("Second");
mut1.ReleaseMutex(); Console.WriteLine("1R");
}
static void fn2()
{
Console.WriteLine("First");
mut2.ReleaseMutex(); Console.WriteLine("2R");
mut1.ReleaseMutex(); Console.WriteLine("1R");
}
}
}
For a start I am not sure you really understand mutexes, you can only release them in the context (i.e. thread) in which they were locked to begin with, so using them as some sort of guard points makes little sense.
It might make more sense to use semaphores in this case. But you still should work out what you are really trying to do :)
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace Test { class Program { static Semaphore sem1 = new Semaphore(1, 1); static Semaphore sem2 = new Semaphore(1, 1); static void Main(string[] args) { sem1.WaitOne(); Console.WriteLine("1W"); sem2.WaitOne(); Console.WriteLine("2W"); Thread oThread = new Thread(new ThreadStart(fn2)); oThread.Start(); sem1.WaitOne(); Console.WriteLine("1W"); Console.WriteLine("Second"); sem1.Release(); Console.WriteLine("1R"); } static void fn2() { Console.WriteLine("First"); sem2.Release(); Console.WriteLine("2R"); sem1.Release(); Console.WriteLine("1R"); } } }
这篇关于如何让我的互斥量不会recusive?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!