原文作者:aircraft

原文链接:https://www.cnblogs.com/DOMLX/p/10945309.html

    

本网络编程入门系列博客是连载学习的,有兴趣的可以看我博客其他篇。。。。c++ 网络编程课设入门超详细教程 ---目录

      最近在找c++服务端开发的实习(大佬们有推荐吗QAQ。。),恰好写了一些c++11多线程有关的东西,就写一下笔记留着以后自己忘记回来看吧,也不是专门写给读者看的,我就想到哪就写到哪吧

  c++11呢,就是c++升级之后的一个版本,现在马上就出c++20了,里面增加了很多对多线程支持的类,让多线程编程更加简单了,好了废话不多说,先来建立一个简单的多线程编程案例,看看c++11下多线程编程创建到底有多么的简单。

1.创建一个简单的多线程案例:

首先导入#include<thread>---用于创建线程

其次导入#include<chrono>--用于时间延时 获取时间之类的

定义一个线程对象t1,这就自动创建了一个线程,参数就是你要线程去执行的函数,t1是变量名字 随便取

std::thread t1(func);

下面这里返回一个毫秒级别的时间间隔参数值,间隔10毫秒

std::chrono::milliseconds(10)

this_thread::sleep_for()就是让此线程休眠,可以传入休眠的时间

this_thread::sleep_for(std::chrono::milliseconds(10));让本线程休眠10毫秒

好了知道这些参数意思就行了,看一下代码:

  1. #include<windows.h>
  2. #include <iostream>
  3. #include <chrono>
  4. #include <thread>
  5. using namespace std;
  6.  
  7. int number = 1;
  8.  
  9. int ThreadProc1()
  10. {
  11. while (number < 100)
  12. {
  13. cout << "thread 1 :" << number << endl;
  14. ++number;
  15. this_thread::sleep_for(std::chrono::milliseconds(10));
  16. }
  17.  
  18. return 0;
  19. }
  20.  
  21. int ThreadProc2()
  22. {
  23. while (number < 100)
  24. {
  25. cout << "thread 2 :" << number << endl;
  26. ++number;
  27. this_thread::sleep_for(std::chrono::milliseconds(10));
  28. }
  29.  
  30. return 0;
  31. }
  32.  
  33. int main()
  34. {
  35. thread t1(ThreadProc1);
  36. thread t2(ThreadProc2);
  37.  
  38. t1.join();
  39. t2.join();
  40.  
  41. system("pause");
  42. return 0;
  43. }

  join()就是阻塞线程,直到线程函数执行完毕,如果函数有返回值,在这里会直接忽略。阻塞的目的就是让Main主线程等待一下创建的线程,免得我函数还在跑,程序就直接结束了。

  如果不想阻塞在这里就将join()换成使用线程的detach()方法,将线程与线程对象分离,线程就可以继续运行下去,并且不会造成影响。

  从示例可以看到c++11下创建多线程多么方便了吧 ,比在Linux下用posix创建还简便,而这个也是可以在windows使用的(想想windows下多线程的代码,看着都头疼好吧,乱七八糟一大堆)。

2.互斥量的使用

  跟往常的多线程一样,多线程在运行过程中都会对临界区进行访问,也就是一起访问共享资源。这样就会造成一个问题,当两个线程都要对一个变量int
value值假如为11,加一时,线程一取出11
进行加一还没有存入value,这时候线程二又取得value的11进行加一,然后线程一存入12,线程二又存入12,这就导入两个线程访问冲突,也就是临界区问题。所以引进互斥量来解决。

导入#include <mutex>

代码案例:

一个线程对变量number进行加一100次,另外一个减一100次,最后结果应该还是原来的值0。

  1. #include<windows.h>
  2. #include <iostream>
  3. #include <chrono>
  4. #include <thread>
  5. #include <mutex>
  6. using namespace std;
  7.  
  8. int number = 0;
  9. mutex g_lock;
  10.  
  11. int ThreadProc1()
  12. {
  13.  
  14. for (int i = 0; i < 100; i++)
  15. {
  16. g_lock.lock();
  17. ++number;
  18. cout << "thread 1 :" << number << endl;
  19. g_lock.unlock();
  20. this_thread::sleep_for(std::chrono::milliseconds(10));
  21. }
  22.  
  23. return 0;
  24. }
  25.  
  26. int ThreadProc2()
  27. {
  28.  
  29. for (int i = 0; i < 100; i++)
  30. {
  31. g_lock.lock();
  32. --number;
  33. cout << "thread 2 :" << number << endl;
  34. g_lock.unlock();
  35. this_thread::sleep_for(std::chrono::milliseconds(10));
  36. }
  37.  
  38. return 0;
  39. }
  40.  
  41. int main()
  42. {
  43. thread t1(ThreadProc1);
  44. thread t2(ThreadProc2);
  45.  
  46. t1.detach();
  47. t2.detach();
  48.  
  49. system("pause");
  50. return 0;
  51. }

上面的每次都要对mutex变量进行锁以及解锁,有时候忘记解锁就凉凉了。所以c++11还提供了一个lock_guard类,它利用了RAII机制可以保证安全释放mutex。

在std::lock_guard对象构造时,传入的mutex对象(即它所管理的mutex对象)会被当前线程锁住。在lock_guard对象被析构时,它所管理的mutex对象会自动解锁,不需要程序员手动调用lock和unlock对mutex进行上锁和解锁操作。lock_guard对象并不负责管理mutex对象的生命周期,lock_guard对象只是简化了mutex对象的上锁和解锁操作,方便线程对互斥量上锁,即在某个lock_guard对象的生命周期内,它所管理的锁对象会一直保持上锁状态;而lock_guard的生命周期结束之后,它所管理的锁对象会被解锁。程序员可以非常方便地使用lock_guard,而不用担心异常安全问题。

代码:

  1. #include<windows.h>
  2. #include <iostream>
  3. #include <chrono>
  4. #include <thread>
  5. #include <mutex>
  6. using namespace std;
  7.  
  8. int number = 0;
  9. mutex g_lock;
  10.  
  11. int ThreadProc1()
  12. {
  13. lock_guard<mutex> loker(g_lock);
  14. for (int i = 0; i < 100; i++)
  15. {
  16. ++number;
  17. cout << "thread 1 :" << number << endl;
  18.  
  19. }
  20. //this_thread::sleep_for(std::chrono::milliseconds(100));
  21. return 0;
  22. }
  23.  
  24. int ThreadProc2()
  25. {
  26. lock_guard<mutex> loker(g_lock);
  27. for (int i = 0; i < 100; i++)
  28. {
  29. --number;
  30. cout << "thread 2 :" << number << endl;
  31. //this_thread::sleep_for(std::chrono::milliseconds(10));
  32. }
  33.  
  34. return 0;
  35. }
  36.  
  37. int main()
  38. {
  39. thread t1(ThreadProc1);
  40. thread t2(ThreadProc2);
  41.  
  42. t1.detach();
  43. t2.detach();
  44.  
  45. system("pause");
  46. return 0;
  47. }

除了lock_guard,之外c++11还提供了std::unique_lock

类 unique_lock 是通用互斥包装器,允许延迟锁定、锁定的有时限尝试、递归锁定、所有权转移和与条件变量一同使用
unique_lock比lock_guard使用更加灵活,功能更加强大。
使用unique_lock需要付出更多的时间、性能成本。

  1. #include <iostream> // std::cout
  2. #include <thread> // std::thread
  3. #include <mutex> // std::mutex, std::unique_lock
  4. #include <vector>
  5.  
  6. std::mutex mtx; // mutex for critical section
  7. std::once_flag flag; //定义一个once_flag类型的变量作为call_once参数,
  8. //用std::call_once来保证多线程环境中只被调用一次
  9. void print_block (int n, char c) {
  10. //unique_lock有多组构造函数, 这里std::defer_lock不设置锁状态
  11. std::unique_lock<std::mutex> my_lock (mtx, std::defer_lock);
  12. //尝试加锁, 如果加锁成功则执行
  13. //(适合定时执行一个job的场景, 一个线程执行就可以, 可以用更新时间戳辅助)
  14. if(my_lock.try_lock()){
  15. for (int i=0; i<n; ++i)
  16. std::cout << c;
  17. std::cout << '\n';
  18. }
  19. }
  20.  
  21. void run_one(int &n){
  22. std::call_once(flag, [&n]{n=n+1;}); //只执行一次, 适合延迟加载; 多线程static变量情况
  23. }
  24.  
  25. int main () {
  26. std::vector<std::thread> ver;
  27. int num = 0;
  28. for (auto i = 0; i < 10; ++i){
  29. ver.emplace_back(print_block,50,'*');
  30. ver.emplace_back(run_one, std::ref(num));
  31. //emplace_back比push_back更好 是c++11增加的
  32. }
  33.  
  34. for (auto &t : ver){
  35. t.join();
  36. }
  37. std::cout << num << std::endl;
  38. return 0;
  39. }

 

 这里还要补充一下跟互斥量很像的条件变量的知识。

条件变量std::condition_variable的使用

  std::condition_variable 是为了解决死锁而生的。当互斥操作不够用而引入的。比如,线程可能需要等待某个条件为真才能继续执行,而一个忙等待循环中可能会导致所有其他线程都无法进入临界区使得条件为真时,就会发生死锁。所以,condition_variable实例被创建出现主要就是用于唤醒等待线程从而避免死锁。std::condition_variable的 notify_one()用于唤醒一个线程;notify_all() 则是通知所有线程。
C++11中的std::condition_variable就像Linux下使用pthread_cond_wait和pthread_cond_signal一样,可以让线程休眠,直到别唤醒,现在在从新执行。线程等待在多线程编程中使用非常频繁,经常需要等待一些异步执行的条件的返回结果。
示例代码:

  1. #include<iostream>
  2. #include<thread>
  3. #include<condition_variable>
  4. #include<mutex>
  5. #include<chrono>
  6.  
  7. std::mutex g_mu;
  8. std::condition_variable g_vc;
  9. bool g_ready = false;
  10.  
  11. void dispaly_id(int id)
  12. {
  13. std::unique_lock<std::mutex> lck(g_mu);
  14. g_vc.wait(lck, []() {return g_ready; }); //线程阻塞,直到第二个参数返回值为真
  15.  
  16. std::cout << "id:" << id << std::endl;
  17. }
  18.  
  19. void ready()
  20. {
  21. std::unique_lock<std::mutex> lck(g_mu);
  22. g_ready = true;
  23. g_vc.notify_all(); //唤醒所有的等待线程
  24. }
  25.  
  26. int main()
  27. {
  28. std::thread t[8];
  29. for (int i = 0; i < 8; i++)
  30. {
  31. t[i] = std::thread(dispaly_id, i);
  32. }
  33. std::this_thread::sleep_for(std::chrono::seconds(3));
  34. std::cout << "all thread lock......" << std::endl;
  35.  
  36. ready();
  37. for (auto & th : t) th.join();
  38.  
  39. system("pause");
  40.  
  41. return 0;
  42. }

3.原子变量的使用

  在新标准C++11,引入了原子操作的概念,原子操作更接近内核,并通过这个新的头文件提供了多种原子操作数据类型,例如,atomic_bool,atomic_int等等,如果我们在多个线程中对这些类型的共享资源进行操作,编译器将保证这些操作都是原子性的,也就是说,确保任意时刻只有一个线程对这个资源进行访问,编译器将保证,多个线程访问这个共享资源的正确性。从而避免了锁的使用,提高了效率。

  上面我们用互斥锁来实现加一百次,减少一百次。使用原子变量会更加简洁。

  1. #include<windows.h>
  2. #include <iostream>
  3. #include <chrono>
  4. #include <thread>
  5. #include <mutex>
  6. #include <atomic>
  7. using namespace std;
  8.  
  9. atomic<int> number(0);//定义原子变量 一次只允许一个线程对其进行访问
  10. //int number = 0;
  11. //mutex g_lock;
  12.  
  13. int ThreadProc1()
  14. {
  15. //lock_guard<mutex> loker(mutex);
  16. for (int i = 0; i < 100; i++)
  17. {
  18. ++number;
  19. cout << "thread 1 :" << number << endl;
  20.  
  21. }
  22. //this_thread::sleep_for(std::chrono::milliseconds(100));
  23. return 0;
  24. }
  25.  
  26. int ThreadProc2()
  27. {
  28. //lock_guard<mutex> loker(mutex);
  29. for (int i = 0; i < 100; i++)
  30. {
  31. --number;
  32. cout << "thread 2 :" << number << endl;
  33. //this_thread::sleep_for(std::chrono::milliseconds(10));
  34. }
  35.  
  36. return 0;
  37. }
  38.  
  39. int main()
  40. {
  41. thread t1(ThreadProc1);
  42. thread t2(ThreadProc2);
  43.  
  44. t1.detach();
  45. t2.detach();
  46.  
  47. system("pause");
  48. return 0;
  49. }

可以看到使用了原子变量之后,代码简化了很多,以及以后对某些共享资源我们都可以酌情的定义为原子变量类型,很方便有木有。。。。。

 4.future与promise的使用

  在c++11中增加的线程库很方便的让我们去使用线程,但是因为做出了一些改变,我们并不能像往常一样直接使用thread.join()获取线程函数的返回值了,而我们有时候又确实要利用线程函数的返回值。

  而thread库提供了future用来访问异步操作的结果,因为一个异步操作的结果往往不能立即获取,只能在未来的某个时候从某个地方获取,这个异步操作的结果是一个未来的期待值,所以被称为future

  future和promise的作用是在不同线程之间传递数据。

假设线程1需要线程2的数据,那么组合使用方式如下:

  1. 线程1初始化一个promise对象和一个future对象,promise传递给线程2,相当于线程2对线程1的一个承诺;future相当于一个接受一个承诺,用来获取未来线程2传递的值
  2. 线程2获取到promise后,需要对这个promise传递有关的数据,之后线程1的future就可以获取数据了。
  3. 如果线程1想要获取数据,而线程2未给出数据,则线程1阻塞,直到线程2的数据到达

示例代码:

  1. #include <iostream>
  2. #include <chrono>
  3. #include <thread>
  4. #include <mutex>
  5. #include <atomic>
  6. #include <future>
  7. #include <vector>
  8.  
  9. void disPlay(std::future<int>& value)
  10. {
  11. std::cout << "wait some times......" << std::endl;
  12. auto result = value.get(); //没有获取到值会阻塞等待获取
  13. std::cout << "Value:" << result << std::endl;
  14. }
  15.  
  16. int main()
  17. {
  18. std::promise<int> promise;
  19. std::future<int> value = promise.get_future(); //将promise与future绑定
  20.  
  21. std::thread t1(disPlay, std::ref(value)); //创建线程并且函数传参,ref()是传一个引用
  22. std::this_thread::sleep_for(std::chrono::seconds(1)); //线程延时1秒
  23.  
  24. //给线程传值进去
  25. promise.set_value(15);
  26. t1.join();
  27.  
  28. system("pause");
  29.  
  30. return 0;
  31. }

  获取future的结果有三种方式上面是get()获取异步结果值返回,还有wait()等待异步操作完成,以及wait_for()超时等待返回结果。

 5.future与package_task的使用

  std::packaged_task包装一个可调用的对象,并且允许异步获取该可调用对象产生的结果。
std::packaged_task将其包装的可调用对象的执行结果传递给一个std::future对象,与std::promise某种程度上是很像的,promise保存一个共享状态的值,而package_task保存的是一个函数。

示例代码:

  1. #include <iostream>
  2. #include <chrono>
  3. #include <thread>
  4. #include <mutex>
  5. #include <atomic>
  6. #include <future>
  7. #include <vector>
  8.  
  9. inline int func(int x)
  10. {
  11. return x + 6;
  12. }
  13.  
  14. int main()
  15. {
  16. std::packaged_task<int(int)> tsk(func);
  17. std::future<int> fut = tsk.get_future(); //获取future绑定起来
  18.  
  19. std::thread(std::move(tsk), 2).detach();//直接将task转移作为线程函数使用
  20.  
  21. auto value = fut.get();
  22. std::cout << "result:" << value << std::endl;
  23.  
  24. system("pause");
  25.  
  26. return 0;
  27. }

 6.线程异步操作函数async的用法

  ,std::async比std::packaged_task,std::promise中,std::thread更高一层,它可以直接用来创建异步的task,异步的结果也保存在future中。完成后,外面再通过future.get/wait来获取这个未来的结果,强烈推荐使用async,我们不需要关注异步任务的结果,只要等待任务完成获取值就行了。

  现在来看看std::async的原型async(std::launch::async | std::launch::deferred, f, args...),第一个参数是线程的创建策略,有两种策略,默认的策略是立即创建线程:

  • std::launch::async:在调用async就开始创建线程。
  • std::launch::deferred:延迟加载方式创建线程。调用async时不创建线程,直到调用了future的get或者wait时才创建线程。

第二个参数是线程函数,第三个参数是线程函数的参数。

代码示例:

  1. #include <iostream>
  2. #include <chrono>
  3. #include <thread>
  4. #include <mutex>
  5. #include <atomic>
  6. #include <future>
  7. #include <vector>
  8.  
  9. int main()
  10. {
  11. std::future<int> fut = std::async(std::launch::async, []() {
  12. return 9;
  13. });
  14.  
  15. std::cout << "result:" << fut.get() << std::endl;
  16. system("pause");
  17.  
  18. return 0;
  19. }

  []()这是c++11里面lambda表达式用法

7.std::future::wait_for()函数作用

  函数原型:

template< class Rep, class Period >
std::future_status wait_for( const std::chrono::duration<Rep,Period>& timeout_duration ) const;
 

  等待结果变得可用。阻塞直至经过指定的 timeout_duration ,或结果变为可用,两者的先到来者。返回值鉴别结果的状态。

此函数可能由于调度或资源争议延迟而阻塞长于 timeout_duration

推荐标准库用稳定时钟度量时长。若实现用系统时钟代替,则等待时间可能也对时钟调整敏感。

若调用此函数前 valid()== false 则行为未定义。

参数

timeout_duration - 要阻塞的最大时长

返回值

 
常量 解释
future_status::deferred 要计算结果的函数仍未启动
future_status::ready 结果就绪
future_status::timeout 已经过时限

异常

时钟、时间点或时长在执行中可能抛的任何异常(标准库提供的时钟、时间点和时长决不抛出)。

注意

鼓励实现在调用前检测 valid == false 的情况并抛出以 future_errc::no_state 为 error_condition 的 future_error

代码示例:

  1. #include <iostream>
  2. #include <future>
  3. #include <thread>
  4. #include <chrono>
  5.  
  6. int main()
  7. {
  8. std::future<int> future = std::async(std::launch::async, [](){
  9. std::this_thread::sleep_for(std::chrono::seconds(3));
  10. return 8;
  11. });
  12.  
  13. std::cout << "waiting...\n";
  14. std::future_status status;
  15. do {
  16. status = future.wait_for(std::chrono::seconds(1));
  17. if (status == std::future_status::deferred) {
  18. std::cout << "deferred\n";
  19. } else if (status == std::future_status::timeout) {
  20. std::cout << "timeout\n";
  21. } else if (status == std::future_status::ready) {
  22. std::cout << "ready!\n";
  23. }
  24. } while (status != std::future_status::ready);
  25.  
  26. std::cout << "result is " << future.get() << '\n';
  27. }

可能结果:

  1. waiting...
  2. timeout
  3. timeout
  4. ready!
  5. result is

后面还会出很多一系列的入门教程,可以关注我噢。(我博客难道写的不清楚吗,你们还不关注我???小声bb)。。。。hhhhhhhh

也可以补一下基础多线程编程教程如下:

c++ 网络编程课设入门超详细教程 ---目录

若有兴趣交流分享技术,可关注本人公众号,里面会不定期的分享各种编程教程,和共享源码,诸如研究分享关于c/c++,python,前端,后端,opencv,halcon,opengl,机器学习深度学习之类有关于基础编程,图像处理和机器视觉开发的知识

c++11 多线程入门教程(一)的更多相关文章

  1. testng入门教程12 TestNG执行多线程测试

    testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...

  2. ABP入门教程11 - 展示层实现增删改查-视图

    点这里进入ABP入门教程目录 创建目录 在展示层(即JD.CRS.Web.Mvc)的Views下新建文件夹Course //用以存放Course相关视图 创建视图 在JD.CRS.Web.Mvc/Vi ...

  3. Kotlin入门教程——目录索引

    Kotlin是谷歌官方认可的Android开发语言,Android Studio从3.0版本开始就内置了Kotlin,所以未来在App开发中Kotlin取代Java是大势所趋,就像当初Android ...

  4. 资深程序员的Metal入门教程总结

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由落影发表于云+社区专栏 正文 本文介绍Metal和Metal Shader Language,以及Metal和OpenGL ES的差异 ...

  5. 我看到的最棒的Twisted入门教程!

    http://www.douban.com/note/232204441/ http://www.cnblogs.com/sevenyuan/archive/2010/11/18/1880681.ht ...

  6. powershell入门教程-v0.3版

    powershell入门教程-v0.3版 来源 https://www.itsvse.com/thread-3650-1-1.html 参考 http://www.cnblogs.com/piapia ...

  7. 学习Objective-C入门教程(分享)

    原百度文库连接:http://wenku.baidu.com/view/6786064fe518964bcf847c63.html PS:需要原文档的可以留邮箱发送! (我叫雷锋,不要谢我) 学习Ob ...

  8. linux.linuxidc.com - /2011年资料/Android入门教程/

    本文转自 http://itindex.net/detail/15843-linux.linuxidc.com-%E8%B5%84%E6%96%99-android Shared by Yuan 用户 ...

  9. gulp详细入门教程

    本文链接:http://www.ydcss.com/archives/18 gulp详细入门教程 简介: gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优 ...

随机推荐

  1. druid数据源的加密解密工具

    数据库得加密 先来一个网上大多数的教程吧,一个比较好的教程,如下. jar包版本:druid-1.0.13.jar 1. 加密,用以下命令将用户名和密码加密 cmd命令行执行 java -cp D:/ ...

  2. 问题11:如何进行反向迭代 & 如何实现反向迭代

    # 有关列表问题,参考:Python:列表list 案例: 需求:实现一个连续浮点数发生器FloatRange(和range类似),根据给定范围(start,end)和步进值(step),产生一系列连 ...

  3. 【转】Pro Android学习笔记(十二):了解Intent(下)

    解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...

  4. 问题:C# List;结果:C#中数组、ArrayList和List三者的区别

    C#中数组.ArrayList和List三者的区别 分类: [C#那些事] 2013-03-11 00:03 36533人阅读 评论(23) 收藏 举报 目录(?)[+] 在C#中数组,ArrayLi ...

  5. 第三波精品Android源码袭来!免费下载

    今天又汇总了一些源码供大家免费下载学习! 1.Android实现NewQuickAction快捷菜单NewQuickAction能根据点击事件发生的坐标来显示一个快捷菜单,比如点击位置在靠近底部,则弹 ...

  6. maven spring3.2.5

    出现的情形: 开发环境: spring3.2.5 + springmvc +spirngDATA +maven 一. 偶然的spring Junit4测试 加载applicationContext.x ...

  7. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; che

    出现此种错误,我暂时遇到了两次. 1 我的字段的名称和数据库的关键字重合. 上图中的desc是默认降序排列的意思. 2 第二次出现的异常是我在重构代码阶段遇到的一个bug.不过我暂时不能理解,虽然解决 ...

  8. 【总结整理】JQuery小技巧

    var item=$("#content").find(".item");//效率最高 var item=$("#content .item" ...

  9. tensorflow session会话控制

    import tensorflow as tf # create two matrixes matrix1 = tf.constant([[3,3]]) matrix2 = tf.constant([ ...

  10. Python最小二乘法解非线性超定方程组

    求解非线性超定方程组,网上搜到的大多是线性方程组的最小二乘解法,对于非线性方程组无济于事. 这里分享一种方法:SciPy库的scipy.optimize.leastsq函数. import numpy ...