我是靠谱客的博主 生动小懒猪,最近开发中收集的这篇文章主要介绍G++下如何编译运行C++11多线程代码,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

安装G++的指令:

1. 编辑源程序:sudo apt-get install g++

    vim hello.cpp

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <thread>  
  3.   
  4. void func(int x)  
  5. {  
  6.     std::cout << x << "    new threadn";  
  7. }  
  8.   
  9. int main()  
  10. {  
  11.     std::cout << "hello worldn";  
  12.   
  13.     std::thread t(func, 8);  
  14.     t.join();  
  15.   
  16.     std::cout << "end threadn";  
  17.   
  18.     return 0;  
  19. }  

2. 编译程序

    g++ -std=c++0x -pthread  hello.cpp -o hello

3. 运行程序

    ./hello


注意编译的时候所带的参数,若不带-pthread,编译可以通过,运行会出现以下问题。

[plain] view plain copy
  1. terminate called after throwing an instance of 'std::system_error'  
  2.   what():  Operation not permitted  
  3. Aborted (core dumped) 

最后

以上就是生动小懒猪为你收集整理的G++下如何编译运行C++11多线程代码的全部内容,希望文章能够帮你解决G++下如何编译运行C++11多线程代码所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部