分享

C++之多线程(C++11 thread.h文件实现多线程)

 禁忌石 2017-05-30
2016-06-14 21:30 6344人阅读 评论(2) 收藏 举报
 分类:

转载自:

http://www.cnblogs.com/haippy/p/3235560.html

http://www.cnblogs.com/lidabo/p/3908705.html

与 C++11 多线程相关的头文件
C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是<atomic> ,<thread>,<mutex>,<condition_variable>和<future>。
<atomic>:该头文主要声明了两个类, std::atomic 和 std::atomic_flag,另外还声明了一套 C 风格的原子类型和与 C 兼容的原子操作的函数。
<thread>:该头文件主要声明了 std::thread 类,另外 std::this_thread 命名空间也在该头文件中。
<mutex>:该头文件主要声明了与互斥量(mutex)相关的类,包括 std::mutex 系列类,std::lock_guard, std::unique_lock, 以及其他的类型和函数。
<condition_variable>:该头文件主要声明了与条件变量相关的类,包括 std::condition_variable 和 std::condition_variable_any。
<future>:该头文件主要声明了 std::promise, std::package_task 两个 Provider 类,以及 std::future 和 std::shared_future 两个 Future 类,另外还有一些与之相关的类型和函数,std::async() 函数就声明在此头文件中。
demo1:thread "Hello world"

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <iostream> // std::cout  
  4. #include <thread>   // std::thread  
  5. void thread_task() {  
  6.     std::cout << "hello thread" << std::endl;  
  7. }  
  8. int main(int argc, const char *argv[])  
  9. {  
  10.     std::thread t(thread_task);  
  11.     t.join();//和主线程协同  
  12.     return EXIT_SUCCESS;  
  13. }  
demo2:一次启动多个线程
我们通常希望一次启动多个线程,来并行工作。为此,我们可以创建线程组,而不是在先前的举例中那样创建一条线程。下面的例子中,主函数创建十条为一组的线程,并且等待这些线程完成他们的任务
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <iostream> // std::cout  
  4. #include <thread>   // std::thread  
  5. int main() {  
  6.     std::thread t[num_threads];  
  7.     //Launch a group of threads 启动一组线程  
  8.     for (int i = 0; i < num_threads; ++i) {  
  9.         t[i] = std::thread(call_from_thread);  
  10.     }  
  11.     std::cout << "Launched from the mainn";  
  12.     //Join the threads with the main thread  
  13.     for (int i = 0; i < num_threads; ++i) {  
  14.         t[i].join();  
  15.     }  
  16.     return 0;  
  17. }  

记住,主函数也是一条线程,通常叫做主线程,所以上面的代码实际上有11条线程在运行。在启动这些线程组之后,线程组和主函数进行协同(join)之前,允许我们在主线程中做些其他的事情。
demo3:在线程中使用带有形参的函数
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <iostream> // std::cout  
  4. #include <thread>   // std::thread  
  5. static const int num_threads = 10;  
  6. //This function will be called from a thread  
  7. void call_from_thread(int tid) {  
  8.     std::cout << "Launched by thread " << tid << std::endl;  
  9. }  
  10. int main() {  
  11.     std::thread t[num_threads];  
  12.     //Launch a group of threads  
  13.     for (int i = 0; i < num_threads; ++i) {  
  14.         t[i] = std::thread(call_from_thread, i);  
  15.     }  
  16.     std::cout << "Launched from the mainn";  
  17.     //Join the threads with the main thread  
  18.     for (int i = 0; i < num_threads; ++i) {  
  19.         t[i].join();  
  20.     }  
  21.     return 0;  
  22. }  
运行结果:
[plain] view plain copy
  1. Sol$ ./a.out  
  2.   
  3.   
  4. Launched by thread 0  
  5.   
  6.   
  7. Launched by thread 1  
  8.   
  9.   
  10. Launched by thread 2  
  11.   
  12.   
  13. Launched from the main  
  14.   
  15.   
  16. Launched by thread 3  
  17.   
  18.   
  19. Launched by thread 5  
  20.   
  21.   
  22. Launched by thread 6  
  23.   
  24.   
  25. Launched by thread 7  
  26.   
  27.   
  28. Launched by thread Launched by thread 4  
  29.   
  30.   
  31. 8L  
  32.   
  33.   
  34. aunched by thread 9  
  35.   
  36.   
  37. Sol$  
能看到上面的结果中,程序一旦创建一条线程,其运行存在先后秩序不确定的现象。程序员的任务就是要确保这组线程在访问公共数据时不要出现阻塞。最后几行,所显示的错乱输出,表明8号线程启动的时候,4号线程还没有完成在stdout上的写操作。事实上假定在你自己的机器上运行上面的代码,将会获得全然不同的结果,甚至是会输出些混乱的字符。原因在于,程序内的11条线程都在竞争性地使用stdout这个公共资源(案:Race Conditions)。
要避免上面的问题,可以在代码中使用拦截器(barriers),如std:mutex,以同步(synchronize)的方式来使得一群线程访问公共资源,或者,如果可行的话,为线程们预留下私用的数据结构,避免使用公共资源。我们在以后的教学中,还会讲到线程同步问题,包括使用原子操作类型(atomic types)和互斥体(mutex)。

更多内容请参考:

C++11 并发指南一(C++11 多线程初探)
http://www.cnblogs.com/haippy/p/3235560.html
C++11 并发指南二(std::thread 详解)
http://www.cnblogs.com/haippy/p/3236136.html
C++11 并发指南三(std::mutex 详解)
http://www.cnblogs.com/haippy/p/3237213.html

C++11 并发指南三(Lock 详解)
http://www.cnblogs.com/haippy/p/3346477.html

C++11 并发指南四(<future> 详解一 std::promise 介绍)
http://www.cnblogs.com/haippy/p/3239248.html

C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)
http://www.cnblogs.com/haippy/p/3279565.html

C++11 并发指南四(<future> 详解三 std::future & std::shared_future)
http://www.cnblogs.com/haippy/p/3280643.html

C++11 并发指南五(std::condition_variable 详解)
http://www.cnblogs.com/haippy/p/3252041.html

C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍)
http://www.cnblogs.com/haippy/p/3252056.html

C++11 并发指南六( <atomic> 类型详解二 std::atomic )
http://www.cnblogs.com/haippy/p/3301408.html

C++11 并发指南六(atomic 类型详解三 std::atomic (续))
http://www.cnblogs.com/haippy/p/3304556.html

C++11 并发指南六(atomic 类型详解四 C 风格原子操作介绍)
http://www.cnblogs.com/haippy/p/3306625.html

C++11 并发指南七(C++11 内存模型一:介绍)
http://www.cnblogs.com/haippy/p/3412858.html

C++11 并发指南九(综合运用: C++11 多线程下生产者消费者模型详解)
http://www.cnblogs.com/haippy/p/3252092.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多