首先mutx m = new mutex();
在一个函数中 m.waitone();
然后 m.releasemutex();
在另一个函数中 同样 m.waitone();
m.releasemutex();
你要写的只能一个进程访问的代码段就放在m.waitone();和m.releasemutex();中间
private mutex mutf = new mutex();
private mutex muth = new mutex();
private void readf()
{
mutf.waitone();
// your code to access the resource
mutf.releasemutex();
}
private void readh()
{
muth.waitone();
// your code to access the resource
muth.releasemutex();
}
private void form1_load(object sender, eventargs e)
{
thread tf = new thread(new threadstart(readf));
thread th = new thread(new threadstart(readh));
tflower.start();
th.start();
mutf.waitone();
muth.waitone();
// your code to access the resource
thread.sleep(1000);
muth.releasemutex();
mutf.releasemutex();
}
以上就是c#thread同步mutex的代码详解的详细内容。