您的位置:首页 > 编程语言 > C语言/C++

gcc g++ 支持 c++0x的办法 实现多线程

2014-04-16 23:09 302 查看
在gcc后面加上parameter -std=c++0x就可以了

c++0x的库有thread和mutex了

g++ -Wl,--no-as-needed -std=c++11 -pthread


g++ -Wl,--no-as-needed -std=c++11 -pthread


C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用起来都比较复杂,C++11提供了新头文件<thread>、<mutex>、<atomic>、<future>等用于支持多线程。

使用C++11开启一个线程是比较简单的,下面来看一个简单的例子:

#include <thread>
#include <iostream>
 
void hello()
{
    std::cout << "Hello from thread " << std::endl;
}
 
int main()
{
    std::thread t1(hello);
    t1.join();
std::cout<<"Main Thread"<<std::endl;
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐