借助在<thread>頭文件中定義的C++線程庫,啟動新的線程將變得非常容易。
通過函數(shù)指針創(chuàng)建線程
通過函數(shù)對象創(chuàng)建線程
通過lambda創(chuàng)建線程
通過成員函數(shù)創(chuàng)建線程
1 通過函數(shù)指針創(chuàng)建線程
#include <iostream>#include <thread>using namespace std;void counter(int id, int numIterations){ for (int i = 0; i < numIterations; ++i) { cout << "Counter " << id << " has value " << i << endl; }}int main(){ thread t1(counter, 1, 6); thread t2(counter, 2, 4); t1.join(); t2.join(); return 0;}/*Counter 2 has value 0Counter 2 has value 1Counter 2 has value 2Counter 2 has value 3Counter 1 has value 0Counter 1 has value 1Counter 1 has value 2Counter 1 has value 3Counter 1 has value 4Counter 1 has value 5 *
2 通過函數(shù)對象創(chuàng)建線程
#include <thread>#include <iostream>using namespace std;class Counter{public: Counter(int id, int numIterations) : mId(id), mNumIterations(numIterations) { } void operator()() const { for (int i = 0; i < mNumIterations; ++i) { cout << "Counter " << mId << " has value " << i << endl; } }private: int mId; int mNumIterations;};int main(){ // Using uniform initialization syntax thread t1{ Counter{ 1, 20 } }; // Using named variable Counter c(2, 12); thread t2(c); // Using temporary thread t3(Counter(3, 10)); // Wait for threads to finish t1.join(); t2.join(); t3.join(); return 0;}/*Counter 1 has value 0Counter 1 has value 1Counter 1 has value 2Counter 1 has value 3Counter 1 has value 4Counter 1 has value 5Counter 1 has value 6Counter 1 has value 7Counter 1 has value 8Counter 1 has value 9Counter 1 has value 10Counter 1 has value 11Counter 1 has value 12Counter 1 has value 13Counter 1 has value 14Counter 1 has value 15Counter 1 has value 16Counter 1 has value 17Counter 1 has value 18Counter 1 has value 19 Counter 3 has value 1Counter 3 has value 2Counter 3 has value 3Counter 3 has value 4Counter 3 has value 5Counter 3 has value 6Counter 3 has value 7Counter 3 has value 8Counter 3 has value 9Counter 2 has value 0Counter 2 has value 1Counter 2 has value 2Counter 2 has value 3Counter 2 has value 4Counter 2 has value 5Counter 2 has value 6Counter 2 has value 7Counter 2 has value 8Counter 2 has value 9Counter 2 has value 10Counter 2 has value 11*/
3 通過lambda創(chuàng)建線程
#include <thread>#include <iostream>using namespace std;int main(){ int id = 1; int numIterations = 5; thread t1([id, numIterations] { for (int i = 0; i < numIterations; ++i) { cout << "Counter " << id << " has value " << i << endl; } }); t1.join(); return 0;}/*Counter 1 has value 0Counter 1 has value 1Counter 1 has value 2Counter 1 has value 3Counter 1 has value 4*/
4 通過成員函數(shù)創(chuàng)建線程
#include <thread>#include <iostream>using namespace std;class Request{public: Request(int id) : mId(id) { } void process() { cout << "Processing request " << mId << endl; }private: int mId;};int main(){ Request req(100); thread t{ &Request::process, &req }; t.join(); return 0;}//Processing request 100
-End-
本文發(fā)布于:2023-02-28 20:05:00,感謝您對本站的認(rèn)可!
本文鏈接:http://www.newhan.cn/zhishi/a/167765474075966.html
版權(quán)聲明:本站內(nèi)容均來自互聯(lián)網(wǎng),僅供演示用,請勿用于商業(yè)和其他非法用途。如果侵犯了您的權(quán)益請與我們聯(lián)系,我們將在24小時內(nèi)刪除。
本文word下載地址:C語言 多線程(c語言多線程編程).doc
本文 PDF 下載地址:C語言 多線程(c語言多線程編程).pdf
| 留言與評論(共有 0 條評論) |