C语言 多线程(c语言多线程编程)

更新时间:2023-03-01 15:12:20 阅读: 评论:0

借助在<thread>头文件中定义的C++线程库,启动新的线程将变得非常容易。

通过函数指针创建线程

通过函数对象创建线程

通过lambda创建线程

通过成员函数创建线程

1 通过函数指针创建线程

#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 通过函数对象创建线程

#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创建线程

#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 通过成员函数创建线程

#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-

本文发布于:2023-02-28 20:05:00,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/zhishi/a/167765474075966.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:C语言 多线程(c语言多线程编程).doc

本文 PDF 下载地址:C语言 多线程(c语言多线程编程).pdf

标签:多线程   语言
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 实用文体写作网旗下知识大全大全栏目是一个全百科类宝库! 优秀范文|法律文书|专利查询|