C++11多线程的创建

左手的ㄟ右手 2022-06-07 08:45 321阅读 0赞
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <list>
  5. #include <thread>
  6. namespace thread_create {
  7. // 参考:https://www.codeproject.com/Articles/598695/Cplusplus-threads-locks-and-condition-variables
  8. // http://www.cnblogs.com/haippy/p/3284540.html
  9. static void Test1(int x, const std::string& y) {
  10. std::cout << x << y << std::endl;
  11. for (int i = 0; i < 100; ++i) {
  12. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  13. std::cout << i << std::endl;
  14. }
  15. }
  16. // 1.创建线程时可以把多个参数传进去。
  17. static void Demo1() {
  18. std::thread thread(Test1, 1, "test");
  19. for (char i = 'a'; i < 'z'; ++i) {
  20. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  21. std::cout << i << std::endl;
  22. }
  23. // 如果不掉用join或者detach,会抛出异常。
  24. thread.join();
  25. //thread1.detach();
  26. }
  27. //------------------------------------------------------------------
  28. class Class01 {
  29. public:
  30. Class01() {
  31. std::cout << "Class01 construct!" << std::endl;
  32. }
  33. Class01(const Class01& c) {
  34. if (this != &c) {
  35. std::cout << "Class01 copy construct!" << std::endl;
  36. }
  37. }
  38. };
  39. static void Test2(int& x) {
  40. x++;
  41. }
  42. static void Test21(const Class01& c) {
  43. std::cout << "Test21" << std::endl;
  44. }
  45. // 2.参数传值默认的是传值。如果是传递引用,需要使用std::ref,std::cref。
  46. // 也就是说如果线程函数是非const引用,我们传值的时候必须用std::ref,
  47. // 否则会编译出错。
  48. static void Demo2() {
  49. int a = 10;
  50. // (A)编译出错,因为第二个参数不是左值,所以不能直接传给引用。
  51. //std::thread thread(Test2, 2);
  52. // (B)同样编译不过,函数的参数是非const引用,则必须用std::ref。
  53. //std::thread thread(Test2, a);
  54. // (C)编译通过。
  55. //std::thread thread(Test2, std::ref(a));
  56. //thread.join();
  57. // 输出11,说明直接传的引用而不是传值。
  58. //std::cout << a << std::endl;
  59. // (D)实际上这个创建过程有两次参数传递。第一次是将参数传给std::thread
  60. // 的构造函数,第二次是std::thread将参数传给线程函数。
  61. Class01 c;
  62. // 会调用一次Class01的拷贝构造函数,因为将c传给std::thread进行了一次拷贝,
  63. // 而std::thread将c传给Test21没有进行拷贝,因为Test21的参数是const引用。
  64. // 如果将Test21的参数是改为传值,则进行两次拷贝构造。
  65. //std::thread thread(Test21, c);
  66. //thread.join();
  67. // 没有调用拷贝构造。
  68. std::thread thread(Test21, std::ref(c));
  69. thread.join();
  70. }
  71. //------------------------------------------------------------------
  72. // 3.std::this_thread
  73. // get_id: returns the id of the current thread
  74. // yield : tells the scheduler to run other threads and can be used when you are in a busy waiting state
  75. // sleep_for : blocks the execution of the current thread for at least the specified period
  76. // sleep_util : blocks the execution of the current thread until the specified moment of time has been reached
  77. } // namespace thread_create
  78. //------------------------------------------------------------------
  79. int main() {
  80. Demo1();
  81. Demo2();
  82. return 0;
  83. }

发表评论

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

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

相关阅读

    相关 C++11线异常

    一旦开始了线程,需要显示决定要等待线程函数完成或分离它自行完成。如果detach()线程不等待,你要确保通过线程访问的数据是有效的,直至该线程完成为止,例如线程函数持有局部变量

    相关 C++11线使用

    C++11之前,C++语言没有对并发编程提供语言级别的支持,这使得我们在编写可移植的并发程序时,存在诸多不便。现在C++11增加了线程以及线程相关的类,很方便地支持了并发编程,

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

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