C++ 11 多线程学习笔记1 --线程创建

绝地灬酷狼 2023-07-05 11:48 139阅读 0赞

https://www.bilibili.com/video/av39161756?p=1

并发编程

  1. 包括多进程编程和多线程编程

进程之间相互通讯的方法

  1. 文件
  2. 管道
  3. 消息队列

多线程的优点

  1. 线程启动速度快
  2. 轻量级
  3. 开销低

多线程的缺点

  1. 管理较难
  2. 不能在分布式系统下运行

运行环境

  1. VS2013 + 控制台应用程序

1.HelloWord

  1. #include <iostream>
  2. #include <thread>
  3. #include <future>
  4. using namespace std;
  5. void helloworld()
  6. {
  7. cout << "hello world \n";
  8. }
  9. int main()
  10. {
  11. //开启一个线程
  12. thread t(helloworld);
  13. cout << "hello world main thread\n";
  14. t.join();//主线程会等待t结束运行
  15. system("pause");
  16. return 0;
  17. }

在这里插入图片描述


2.使用detach,主线程将不等子线程运行完就结束

  1. #include <iostream>
  2. #include <thread>
  3. #include <future>
  4. using namespace std;
  5. void helloworld()
  6. {
  7. cout << "hello world" << endl;
  8. }
  9. int main()
  10. {
  11. //开启一个线程
  12. thread t(helloworld);
  13. t.detach();
  14. return 0;
  15. }

按照视频里面是不会

会只输出一个h就结束了


3.一个线程先使用了detach之后就不能join否则会报错。所以join之前先进行判断

  1. #include <iostream>
  2. #include <thread>
  3. #include <future>
  4. using namespace std;
  5. void helloworld()
  6. {
  7. cout << "hello world" << endl;
  8. }
  9. int main()
  10. {
  11. //开启一个线程
  12. thread t(helloworld);
  13. t.detach();
  14. if (t.joinable()) {
  15. t.join();
  16. }
  17. return 0;
  18. }

在这里插入图片描述


4.异常处理

  1. #include <iostream>
  2. #include <thread>
  3. #include <future>
  4. using namespace std;
  5. void helloworld()
  6. {
  7. cout << "hello world" << endl;
  8. }
  9. int main()
  10. {
  11. thread t(helloworld);
  12. for (int i = 0; i < 10; i++) {
  13. cout << "this is main thread" << endl;
  14. }
  15. //如果上面的代码抛出异常,t在join之前就会被销毁,这个时候代码就报错了
  16. t.join();
  17. return 0;
  18. }

修改为

  1. #include <iostream>
  2. #include <thread>
  3. #include <future>
  4. using namespace std;
  5. void helloworld()
  6. {
  7. cout << "hello world" << endl;
  8. }
  9. int main()
  10. {
  11. thread t(helloworld);
  12. try {
  13. for (int i = 0; i < 10; i++) {
  14. cout << "this is main thread" << endl;
  15. }
  16. }
  17. catch (...) {
  18. t.join();
  19. throw;
  20. }
  21. t.join();
  22. return 0;
  23. }

不管主线程是否抛出异常,t都可以正常join


5.通过类创建线程

  1. #include <iostream>
  2. #include <thread>
  3. #include <future>
  4. using namespace std;
  5. void helloworld()
  6. {
  7. cout << "hello world" << endl;
  8. }
  9. class Fctor {
  10. public:
  11. void operator()() {
  12. for (int i = 0; i > -10; i--) {
  13. cout << "from t1: " << i << endl;
  14. }
  15. }
  16. };
  17. int main()
  18. {
  19. Fctor fct;
  20. thread t1(fct);
  21. for (int i = 0; i < 10; i++) {
  22. cout << "from main: " << i << endl;
  23. }
  24. //如果上面的代码抛出异常,t在join之前就会被销毁,这个时候代码就报错了
  25. t1.join();
  26. return 0;
  27. }

在这里插入图片描述


6.通过仿函数创建

  1. #include <iostream>
  2. #include <thread>
  3. #include <future>
  4. using namespace std;
  5. class Fctor {
  6. public:
  7. void operator()() {
  8. for (int i = 0; i > -10; i--) {
  9. cout << "from t1: " << i << endl;
  10. }
  11. }
  12. };
  13. int main()
  14. {
  15. Fctor fct;
  16. //thread t1(fct);
  17. //也可以
  18. thread t1((Fctor()));
  19. for (int i = 0; i < 10; i++) {
  20. cout << "from main: " << i << endl;
  21. }
  22. //如果上面的代码抛出异常,t在join之前就会被销毁,这个时候代码就报错了
  23. t1.join();
  24. return 0;
  25. }

7.添加参数进行构造

  1. #include <iostream>
  2. #include <thread>
  3. #include <future>
  4. #include <string>
  5. using namespace std;
  6. class Fctor {
  7. public:
  8. void operator()(std::string msg) {
  9. for (int i = 0; i > -10; i--) {
  10. cout << "from t1: " << msg << endl;
  11. }
  12. }
  13. };
  14. int main()
  15. {
  16. Fctor fct;
  17. string s = "hello world";
  18. thread t1((Fctor()),s);
  19. for (int i = 0; i < 10; i++) {
  20. cout << "from main: " << i << endl;
  21. }
  22. //如果上面的代码抛出异常,t在join之前就会被销毁,这个时候代码就报错了
  23. t1.join();
  24. return 0;
  25. }

8.通过引用传递参数std::ref

  1. #include <iostream>
  2. #include <thread>
  3. #include <future>
  4. #include <string>
  5. using namespace std;
  6. class Fctor {
  7. public:
  8. void operator()(std::string& msg) {
  9. cout << "from t1: " << msg << endl;
  10. msg = "xiugai";
  11. }
  12. };
  13. int main()
  14. {
  15. Fctor fct;
  16. string s = "hello world";
  17. thread t1((Fctor()),std::ref(s));
  18. t1.join();
  19. cout << "from main: " << s << endl;
  20. return 0;
  21. }

在这里插入图片描述


9.不通过引用,(可能导致数据竞争),通过move创建

  1. #include <iostream>
  2. #include <thread>
  3. #include <future>
  4. #include <string>
  5. using namespace std;
  6. class Fctor {
  7. public:
  8. void operator()(std::string msg) {
  9. cout << "from t1: " << msg << endl;
  10. msg = "xiugai";
  11. }
  12. };
  13. int main()
  14. {
  15. Fctor fct;
  16. string s = "hello world";
  17. cout << this_thread::get_id() << endl;
  18. thread t1((Fctor()),std::move(s));
  19. thread t2 = std::move(t1);
  20. t2.join();
  21. cout << "from main: " << s << endl;
  22. cout << "from main: " << t2.get_id()<< endl;
  23. return 0;
  24. }

在这里插入图片描述
move把s移走了


10. hardware_concurrency输出可以并发编程的线程数量

  1. #include <iostream>
  2. #include <thread>
  3. #include <future>
  4. #include <string>
  5. using namespace std;
  6. class Fctor {
  7. public:
  8. void operator()(std::string msg) {
  9. cout << "from t1: " << msg << endl;
  10. msg = "xiugai";
  11. }
  12. };
  13. int main()
  14. {
  15. Fctor fct;
  16. string s = "hello world";
  17. cout << this_thread::get_id() << endl;
  18. thread t1((Fctor()),std::move(s));
  19. t1.join();
  20. cout<<std::thread::hardware_concurrency()<<endl;
  21. return 0;
  22. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,139人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C++ 11 线--线管理

    关于c++11多线程,今天看到一篇讲的很好的文章,感谢原作者的分享,原文地址见 文章末尾 说到多线程编程,那么就不得不提并行和并发,多线程是实现并发(并行)的一种手段。并行是