我是靠谱客的博主 要减肥小天鹅,这篇文章主要介绍C++11 多线程gcc编译简单示例,现在分享给大家,希望可以做个参考。

1. 编辑源程序

    vim hello.cpp

#include <iostream>
#include <thread>

void func(int x)
{
    std::cout << x << "    new threadn";
}

int main()
{
    std::cout << "hello worldn";

    std::thread t(func, 8);
    t.join();

    std::cout << "end threadn";

    return 0;
}

2. 编译程序

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

3. 运行程序

    ./hello


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

terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted
Aborted (core dumped)




最后

以上就是要减肥小天鹅最近收集整理的关于C++11 多线程gcc编译简单示例的全部内容,更多相关C++11内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部