記錄一下c 中,多執行緒中的使用,以及例項

2021-09-25 07:06:17 字數 1301 閱讀 7711

#include #include #include #include #include #include using std::cout;

using std::cin;

using std::endl;

using std::string;

using std::initializer_list;

using namespace std;

//定義乙個測量**執行時間模板

template void measure(t&& func)

void func()

cout << s << endl;

}//測試多執行緒分擔任務..

void funmultex(long start,long end,long& nans)

nans = ann;

}long funsum(long start,long end)

return s;

}const long klong = 1000000000;

int main()

cout << s << endl;

});//正常傳參.

measure(func);

measure(() );

measure(() );

measure(()

long ans = 0;

for (int i = 0; i < k; i++)

cout << "五個執行緒計算的結果: " << ans << endl;

});measure(() );

system("pause");

return 0;

}

src data: i am a hero

move str2: i am a hero

i am a hero bag

499999500000

elapsed: 0.0022347seconds:

4999950000

elapsed: 0.0005302seconds:

輸出結果:-1243309312

elapsed: 1.02473seconds:

輸出結果2: -1243309312

elapsed: 2.02655seconds:

五個執行緒計算的結果: -1243309312

elapsed: 0.536332seconds:

輸出結果2: -1243309312

elapsed: 2.04541seconds:

請按任意鍵繼續. . .

多執行緒環境下fork的使用以及鎖的變化

關於執行緒的建立與使用,前幾個部落格說的很詳細了,那麼我們如果在某乙個函式執行緒中呼叫fork函式有什麼需要注意的嗎?示例 include include include include include include 測試執行緒中呼叫frok的結果 void fun void arg else i...

談一下多執行緒下的count

在單執行緒下,我們經常使用count 將count的值自增1,也不會發生什麼錯誤,但是在多執行緒下,可能使用count 結果可能就往往出乎我們的意料了.我們以count 一直從1加到5為例 public class threadcount extends thread override public...

C 多執行緒中鎖的使用

最近的專案中涉及到實時資料的處理,經常會使用多執行緒訪問共享資源。如果處理不當,資源未能正確在各個執行緒中同步的話,計算結果將會出現錯誤。關於資源同步最常用的技術就是加鎖。這裡提到是乙個比較簡單的鎖 lock。lock是對monitor中的兩個函式enter和exit的封裝。當時專案的模式是這樣的 ...