C 多執行緒學習總結

2021-07-24 02:10:16 字數 2565 閱讀 7632

thread類類位於system.threading命名空間中。

建構函式
public

thread(threadstart start);

public

thread(parameterizedthreadstart start);

public

thread(parameterizedthreadstart start, int maxstacksize);

public

thread(threadstart start, int maxstacksize);

第乙個函式市我們最常用到的函式,這裡介紹一下,引數threadstart是乙個委託型別,該委託型別封裝的方法沒用引數也沒有返回值。初始化執行緒時使用具有相同簽名的函式來建立threadstart委託的委託例項,在將其作為引數作為來初始化該執行緒。那麼一旦執行緒啟動就會執行這個函式。如下:

static void sum()

thread t = new thread(new threadstart(sum)); //建立乙個執行緒例項

執行緒啟動函式

t.start(); //用於啟動乙個執行緒
執行緒掛起函式,執行緒被掛起後,操作被停止或者進入休眠狀態。被掛起的執行緒不會占用任何處理器時間。可以用執行緒恢復函式恢復執行。
t.suspend();//用於掛起執行緒,
執行緒恢復函式,可以用於恢復被掛起的執行緒
t.resume();//恢復執行緒
執行緒中止函式,執行緒被中止,就停止執行,是無法恢復的。因為作業系統會刪除該執行緒所有資訊
t.abort();
執行緒阻塞函式,會阻塞執行緒,當沒有引數時,會阻塞到另乙個執行緒執行完畢,當有引數時會阻塞該執行緒一段時間(由引數決定)
t.join();

t.join(1000); //過載實現public bool joint(int millisecondstimeout)

上述例子完整**

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

using system.threading;

namespace

threadexample1

i : ", sum, i);}}

}static

void que()

i : ", que, i);}}

}static

void main(string args)

}}

這裡乙個引數的話很簡單,用parameterizedthreadstart構造thread建構函式的引數即可,這裡我不列出**。但這樣的話,兩個引數,多個引數呢?最先想到的肯定是用乙個類或者結構體來封裝我們的引數,但有一種更好的辦法是用匿名函式(就像c++中我們最熟悉的函式指標,objective-c中的**塊),**如下 :
using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

using system.threading;

namespace threadexample1

set}

public

string

outputexcel(string a, long? b, string c)

}}using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

using system.threading;

namespace threadexample1

)); t1.start();

var result1 = getresult();

system.console.writeline(result1);

}private

static dictionary map = new dictionary();

private

static

string

getresult()

else

}return result;}}

}

未完成 先留坑

c 多執行緒學習總結

一 建立執行緒 include pthread create thread,attr,start routine,arg 建立執行緒成功時,函式返回 0,若返回值不為 0 則說明建立執行緒失敗。引數描述 thread 指向執行緒識別符號指標。attr 乙個不透明的屬性物件,可以被用來設定執行緒屬性。...

c 多執行緒總結

std thread比較好用,但是系統帶的socket不能呼叫recv handle h thread createthread null,0,fun,null,0,null 建立多執行緒 closehandle h thread 使用者介面執行緒經常過載該函式,工作者執行緒一般不使用 initin...

多執行緒學習總結(一) 認識多執行緒

而唯一看到的多執行緒 也是再專案中看到了別人寫的多執行緒 那時想要寫乙個和多執行緒相關的 基本是一模一樣的仿照著寫,出了一點問題也搞不懂問題在 所以就一直迷迷糊糊了一段時間,後來感覺有必要把這塊搞懂,就自己慢慢的找資料學習了。廢話就講這麼多,其實要學習多執行緒相關的,首先要搞清楚乙個概念,什麼是執行...