C#多線程中如何運(yùn)用互斥鎖Mutex
互斥鎖(Mutex)
互斥鎖是一個(gè)互斥的同步對(duì)象,意味著同一時(shí)間有且僅有一個(gè)線程可以獲取它。
互斥鎖可適用于一個(gè)共享資源每次只能被一個(gè)線程訪問(wèn)的情況
函數(shù):
//創(chuàng)建一個(gè)處于未獲取狀態(tài)的互斥鎖 Public Mutex(); //如果owned為true,互斥鎖的初始狀態(tài)就是被主線程所獲取,否則處于未獲取狀態(tài) Public Mutex(bool owned);
如果要獲取一個(gè)互斥鎖。應(yīng)調(diào)用互斥鎖上的WaitOne()方法,該方法繼承于Thread.WaitHandle類(lèi)
它處于等到狀態(tài)直至所調(diào)用互斥鎖可以被獲取,因此該方法將組織住主調(diào)線程直到指定的互斥鎖可用,如果不需要擁有互斥鎖,用ReleaseMutex方法釋放,從而使互斥鎖可以被另外一個(gè)線程所獲取。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MyTTCon
{
class shareRes
{
public static int count = 0;
public static Mutex mutex = new Mutex();
}
class IncThread
{
int number;
public Thread thrd;
public IncThread(string name, int n)
{
thrd = new Thread(this.run);
number = n;
thrd.Name = name;
thrd.Start();
}
void run()
{
Console.WriteLine(thrd.Name + "正在等待 the mutex");
//申請(qǐng)
shareRes.mutex.WaitOne();
Console.WriteLine(thrd.Name + "申請(qǐng)到 the mutex");
do
{
Thread.Sleep(1000);
shareRes.count++;
Console.WriteLine("In " + thrd.Name + "ShareRes.count is " + shareRes.count);
number--;
} while (number > 0);
Console.WriteLine(thrd.Name + "釋放 the nmutex");
// 釋放
shareRes.mutex.ReleaseMutex();
}
}
class DecThread
{
int number;
public Thread thrd;
public DecThread(string name, int n)
{
thrd = new Thread(this.run);
number = n;
thrd.Name = name;
thrd.Start();
}
void run()
{
Console.WriteLine(thrd.Name + "正在等待 the mutex");
//申請(qǐng)
shareRes.mutex.WaitOne();
Console.WriteLine(thrd.Name + "申請(qǐng)到 the mutex");
do
{
Thread.Sleep(1000);
shareRes.count--;
Console.WriteLine("In " + thrd.Name + "ShareRes.count is " + shareRes.count);
number--;
} while (number > 0);
Console.WriteLine(thrd.Name + "釋放 the nmutex");
// 釋放
shareRes.mutex.ReleaseMutex();
}
}
class Program
{
static void Main(string[] args)
{
IncThread mthrd1 = new IncThread("IncThread thread ", 5);
DecThread mthrd2 = new DecThread("DecThread thread ", 5);
mthrd1.thrd.Join();
mthrd2.thrd.Join();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#多線程中的互斥鎖Mutex
- C#多線程死鎖介紹與案例代碼
- C#在復(fù)雜多線程環(huán)境下使用讀寫(xiě)鎖同步寫(xiě)入文件
- C#使用讀寫(xiě)鎖解決多線程并發(fā)問(wèn)題
- C#多線程系列之讀寫(xiě)鎖
- C#多線程系列之多線程鎖lock和Monitor
- c#多線程之間的排他鎖的實(shí)現(xiàn)
- C#筆試題之同線程Lock語(yǔ)句遞歸不會(huì)死鎖
- C#使用讀寫(xiě)鎖三行代碼簡(jiǎn)單解決多線程并發(fā)的問(wèn)題
- C#多線程編程中的鎖系統(tǒng)(四):自旋鎖
- C#多線程編程中的鎖系統(tǒng)(三)
- C#多線程編程中的鎖系統(tǒng)(二)
- C#多線程編程中的鎖系統(tǒng)基本用法
- C#多線程之線程鎖
相關(guān)文章
C#判斷程序是否是管理員權(quán)限運(yùn)行的方法代碼示例
這篇文章主要介紹了C#判斷程序是否是管理員權(quán)限運(yùn)行的方法代碼示例,本文直接給出實(shí)現(xiàn)代碼例子,需要的朋友可以參考下2015-03-03
詳解C# parallel中并行計(jì)算的四種寫(xiě)法總結(jié)
在C#中,parallel關(guān)鍵字可以用于并行計(jì)算。本文為大家總結(jié)了四種C# parallel并行計(jì)算的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-11-11
C#使用foreach遍歷哈希表(hashtable)的方法
這篇文章主要介紹了C#使用foreach遍歷哈希表(hashtable)的方法,是C#中foreach語(yǔ)句遍歷散列表的典型應(yīng)用,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C#創(chuàng)建WebService接口并連接的全過(guò)程
工作時(shí)遇到需要請(qǐng)求客戶(hù)的接口返回?cái)?shù)據(jù),要求使用WebService,借此機(jī)會(huì)記錄一下,下面這篇文章主要給大家介紹了關(guān)于C#創(chuàng)建WebService接口并連接的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
C# 文件上傳下載(Excel導(dǎo)入,多線程下載)功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了C# 文件上傳下載(Excel導(dǎo)入,多線程下載)功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-08-08

