【问题标题】:Does gcc 4.7.1 support threads?gcc 4.7.1 支持线程吗?
【发布时间】:2012-08-31 06:39:54
【问题描述】:

我按照指令 here 设置 CodeBlocks 以使用 GCC 4.7.1。我提到的页面上提供的示例编译得很好,但是当我尝试编译以下代码时,它随后产生了错误。我必须说我什至通过编译器设置(-std=c++11)设置了编译器 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) ===|

【问题讨论】:

  • 在 Unix 上是的,但在 windows 上没有 mingw。
  • 没错。 GCC 可能在 pthreads 上实现了 std::thread,这意味着它们无法在 Windows Win32 线程上工作。获取 UNIX 系统。
  • 是的,在 Linux 上它依赖于 pthread。顺便说一句,这意味着,在 Linux 上,您还需要在命令行中添加链接器标志 -lpthread。否则它将编译,但不会链接或产生运行时错误。
  • @jogojapan 我认为建议使用-pthread 选项,至少在linux上。
  • @jogojapan -pthread 不仅仅是链接 libpthread,但我现在不记得究竟“更多”是什么。

标签: c++ multithreading gcc mingw


【解决方案1】:

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

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

【讨论】:

  • 比你,但我应该下载哪一个?i686 版本还是 x86-x64?顺便说一句,我使用的是 32 位 Windows。
  • @Hossein:然后获取 i686。另一个是 64 位的。
  • 项目 mingw-builds 已移至 sourceforge.net/projects/mingwbuilds/files/windows-host 此处您可以找到 mingw 的最新版本
  • @AnthonyWilliams: 非常感谢,我照我说的做了,一切都很好;)
【解决方案2】:

MinGW 根本无法使用 '-std=c++0x' 进行编译。很奇怪,'-std=gnu++0x' 有效。

【讨论】:

    【解决方案3】:

    尝试 MinGw 构建:

    http://sourceforge.net/projects/mingwbuilds/

    此安装程序将允许您选择所需的任何 MinGW,还包括 c++11 线程功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 2015-12-20
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      相关资源
      最近更新 更多