c++ - gcc 4.7.1 是否支持线程?

标签 c++ multithreading gcc mingw

我遵循了说明 here关于设置 CodeBlocks 以使用 GCC 4.7.1。我提到的页面上提供的示例编译得很好,但是当我尝试编译以下代码时,它随后产生了错误。我必须说我什至将编译器设置为 C+ +11 标志通过编译器设置 (-std=c++11) 但仍然没有运气。 编译失败的代码:

#include <iostream>
#include <thread>
#include <vector>

//This function will be called from a thread

void func(int tid) {
    std::cout << "Launched by thread " << tid << std::endl;
}

int main() {
    std::vector<std::thread> th;

    int nr_threads = 10;

    //Launch a group of threads
    for (int i = 0; i < nr_threads; ++i) {
        th.push_back(std::thread(func,i));
    }

    //Join the threads with the main thread
    for(auto &t : th){
        t.join();
    }

    return 0;
}

错误:

 main.cpp||In function 'int main()':|
 main.cpp|13|error: 'thread' is not a member of 'std'|
 main.cpp|13|error: 'thread' is not a member of 'std'|
 main.cpp|13|error: template argument 1 is invalid|
 main.cpp|13|error: template argument 2 is invalid|
 main.cpp|13|error: invalid type in declaration before ';' token|
 main.cpp|19|error: request for member 'push_back' in 'th', which is of non-class type 'int'|
 main.cpp|19|error: 'thread' is not a member of 'std'|
 main.cpp|23|error: no matching function for call to 'begin(int&)'|
 main.cpp|23|note: candidates are:|
 \include\c++\4.7.1\initializer_list|89|note: template<class _Tp> constexpr const _Tp* std::begin(std::initializer_list<_Tp>)|
 \include\c++\4.7.1\initializer_list|89|note:   template argument deduction/substitution failed:|
 main.cpp|23|note:   mismatched types 'std::initializer_list<_Tp>' and 'int'|
 \include\c++\4.7.1\bits\range_access.h|87|note: template<class _Tp, unsigned int _Nm> _Tp* std::begin(_Tp (&)[_Nm])|
 \include\c++\4.7.1\bits\range_access.h|87|note:   template argument deduction/substitution failed:|
 main.cpp|23|note:   mismatched types '_Tp [_Nm]' and 'int'|
 \include\c++\4.7.1\bits\range_access.h|58|note: template<class _Container> decltype (__cont.begin()) std::begin(const _Container&)|
 \include\c++\4.7.1\bits\range_access.h|58|note:   template argument deduction/substitution failed:|
 main.cpp|23|required from here|
 \include\c++\4.7.1\bits\range_access.h|58|error: request for member 'begin' in '__cont', which is of non-class type 'const int'|
 \include\c++\4.7.1\bits\range_access.h|48|note: template<class _Container> decltype (__cont.begin()) std::begin(_Container&)|
 \include\c++\4.7.1\bits\range_access.h|48|note:   template argument deduction/substitution failed:|
 main.cpp|23|required from here|
 \include\c++\4.7.1\bits\range_access.h|48|error: request for member 'begin' in '__cont', which is of non-class type 'int'|
 main.cpp|23|error: no matching function for call to 'end(int&)'|
 main.cpp|23|note: candidates are:|
 \include\c++\4.7.1\initializer_list|99|note: template<class _Tp> constexpr const _Tp* std::end(std::initializer_list<_Tp>)|
 \include\c++\4.7.1\initializer_list|99|note:   template argument deduction/substitution failed:|
 main.cpp|23|note:   mismatched types 'std::initializer_list<_Tp>' and 'int'|
 \include\c++\4.7.1\bits\range_access.h|97|note: template<class _Tp, unsigned int _Nm> _Tp* std::end(_Tp (&)[_Nm])|
 \include\c++\4.7.1\bits\range_access.h|97|note:   template argument deduction/substitution failed:|
 main.cpp|23|note:   mismatched types '_Tp [_Nm]' and 'int'|
 \include\c++\4.7.1\bits\range_access.h|78|note: template<class _Container> decltype (__cont.end()) std::end(const _Container&)|
 \include\c++\4.7.1\bits\range_access.h|78|note:   template argument deduction/substitution failed:|
 main.cpp|23|required from here|
 \include\c++\4.7.1\bits\range_access.h|78|error: request for member 'end' in '__cont', which is of non-class type 'const int'|
 \include\c++\4.7.1\bits\range_access.h|68|note: template<class _Container> decltype (__cont.end()) std::end(_Container&)|
 \include\c++\4.7.1\bits\range_access.h|68|note:   template argument deduction/substitution failed:|
 main.cpp|23|required from here|
 \include\c++\4.7.1\bits\range_access.h|68|error: request for member 'end' in '__cont', which is of non-class type 'int'|
 main.cpp|23|error: unable to deduce 'auto&' from '<expression error>'|
||=== Build finished: 14 errors, 4 warnings (0 minutes, 6 seconds) ===|

最佳答案

对于 mingw,gcc 的线程支持取决于您获得的构建。有些构建不支持线程,有些则支持。

我相信 mingw 构建在 http://code.google.com/p/mingw-builds/downloads/list支持线程。

关于c++ - gcc 4.7.1 是否支持线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12210102/

相关文章:

c# - 拥有一个带有 C++ 类的 DLL,即构造函数接受其他 C++ 类,如何使 C# 能够创建其实例?

c++ - 如何创建会抛出堆栈溢出异常的无限递归?

java - 如何使用 volatile 变量编写简单的线程安全类?

在 Eclipse SIGSEV 中编译 C 二进制文件

c - 如何在 C 中创建位图并使用 gcc 进行编译

C++/Visual Studio - _wgetcwd 返回项目文件夹而不是应用程序

c++ - HTTP Content-Length 大小不正确?

c# - C#调用BeginInvoke/Invoke时如何获取返回值

java - 当另一个线程正在使用映射时,重新分配对映射的引用会引发异常吗?

c++ - 为什么模板中的 static_assert 使用等效表达式会给我不同的结果?