C#多線程同步lock、Mutex的實(shí)現(xiàn)
C#使用多線程可以通過(guò)System.Threading命名空間下的Thread類來(lái)實(shí)現(xiàn)
lock和Mutex用于實(shí)現(xiàn)線程同步的機(jī)制:
上代碼:
class People
{
public People(int idd)
{
id = idd;
}
public int id;
public int age;
}
class TestHelper
{
public TestHelper() { }
List<People> m_data = new List<People>();
int m_iComplete;
private void LockThread(object obj)
{
List<object> parameters = (List<object>)obj;
int idx = (int)parameters[0];
while (true)
{
People data = null;
lock(this)
{
if (m_iComplete >= m_data.Count)
{
return;
}
data = m_data[m_iComplete];
Interlocked.Increment(ref m_iComplete);
data.age = data.id;
Console.Write("======");
Console.WriteLine("id:" + data.id.ToString() + ",age:" + data.age.ToString());
}
}
}
//測(cè)試lock
public void TestLock()
{
DateTime time1 = DateTime.Now;
m_iComplete = 0;
m_data.Clear();
for (int i = 0; i < 10000;i++)
{
m_data.Add(new People(i + 1));
}
List<Thread> threads = new List<Thread>();
for (int i = 0; i < Environment.ProcessorCount; i++)
{
Thread th = new Thread(LockThread);
th.IsBackground = true;
List<object> objs = new List<object>();
objs.Add(i + 1);
th.Start(objs);
threads.Add(th);
}
foreach (var th in threads)
{
th.Join();
}
DateTime time2 = DateTime.Now;
TimeSpan deltaTime = time2.Subtract(time1);
Console.Write("===================耗時(shí): ");
Console.WriteLine(deltaTime.TotalSeconds);
}
Mutex m_mutex = new Mutex();
private void MutexThread(object obj)
{
List<object> parameters = (List<object>)obj;
int idx = (int)parameters[0];
while (true)
{
People data = null;
//開啟
m_mutex.WaitOne();
if (m_iComplete >= m_data.Count)
{
//釋放
m_mutex.ReleaseMutex();
return;
}
data = m_data[m_iComplete];
Interlocked.Increment(ref m_iComplete);
data.age = data.id;
Console.Write("======");
Console.WriteLine("id:" + data.id.ToString() + ",age:" + data.age.ToString());
//釋放
m_mutex.ReleaseMutex();
}
}
//測(cè)試mutex
public void TestMutex()
{
DateTime time1 = DateTime.Now;
m_iComplete = 0;
m_data.Clear();
for (int i = 0; i < 10000; i++)
{
m_data.Add(new People(i + 1));
}
List<Thread> threads = new List<Thread>();
for (int i = 0;i<Environment.ProcessorCount;i++)
{
Thread th = new Thread(MutexThread);
List<object> objs = new List<object>();
objs.Add(i + 1);
th.Start(objs);
threads.Add(th);
}
foreach(var th in threads)
{
//同步等待
th.Join();
}
DateTime time2 = DateTime.Now;
TimeSpan deltaTime = time2.Subtract(time1);
int sec = (int)deltaTime.TotalSeconds;
Console.Write("===================耗時(shí): ");
Console.WriteLine(deltaTime.TotalSeconds);
}
}
測(cè)試:

起多個(gè)線程計(jì)算:

總結(jié):
1、Lock是C#中的關(guān)鍵字,用于對(duì)代碼塊進(jìn)行同步。Lock只能用于同一進(jìn)程內(nèi)的線程同步。輕量級(jí)的同步機(jī)制,開銷比較小,使用簡(jiǎn)單,不能用于跨應(yīng)用程序域的線程同步。
2、Mutex是系統(tǒng)級(jí)別的同步對(duì)象,用于跨進(jìn)程的線程同步。 Mutex是內(nèi)核對(duì)象,因此創(chuàng)建和銷毀代價(jià)高,用于跨應(yīng)用程序域的線程同步。
到此這篇關(guān)于C#多線程同步lock、Mutex的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C#多線程同步lock、Mutex內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c# EPPlus秘籍之Excel實(shí)現(xiàn)圖表導(dǎo)出
這篇文章主要為大家介紹了c# EPPlus秘籍之Excel實(shí)現(xiàn)圖表導(dǎo)出示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
基于C#實(shí)現(xiàn)的多邊形沖突檢測(cè)實(shí)例
這篇文章主要給大家介紹了基于C#實(shí)現(xiàn)的多邊形沖突檢測(cè)的相關(guān)資料,文中介紹的方法并未使用第三方類庫(kù),可以完美解決這個(gè)問(wèn)題,需要的朋友可以參考下2021-07-07
C#實(shí)現(xiàn)循環(huán)發(fā)送電腦屏幕截圖
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)循環(huán)發(fā)送電腦屏幕截圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
c# 判斷指定文件是否存在的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了c# 判斷指定文件是否存在的簡(jiǎn)單實(shí)現(xiàn),需要的朋友可以參考下2014-02-02
C#中使用HttpDownLoadHelper下載文件實(shí)例
這篇文章主要介紹了C#中使用HttpDownLoadHelper下載文件的方法,并實(shí)例講述了在webfrom中與在mvc中的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-10-10
C#數(shù)據(jù)綁定(DataBinding)簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了C#數(shù)據(jù)綁定(DataBinding)簡(jiǎn)單實(shí)現(xiàn)方法,以簡(jiǎn)單實(shí)例形式簡(jiǎn)單分析了C#實(shí)現(xiàn)數(shù)據(jù)綁定與讀取的方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C# out關(guān)鍵詞的應(yīng)用實(shí)例
下面小編就為大家分享一篇C# out關(guān)鍵詞的應(yīng)用實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12

