无法解析命名空间成员 'thread'

Can't resolve namespace member 'thread'

我想用标准的 C++ 线程而不是 UNIX 线程来练习,但很快就遇到了一个问题,每当我写 std::thread CLion 用红色下划线并说无法解析命名空间成员 'thread' .我检查了我的 CMake 文件,它是为 C++11 设置的。我重新安装了最新版本的 MinGW (6.3.0) 并勾选了 G++ 编译器。我的朋友告诉我他使用 Cygwin 并且一切正常。但是它仍然可以与 MinGW 一起使用吗?

#include <iostream>
#include <thread>

#define BUFFER_SIZE 3
#define PROD_NUM 3
#define CONS_NUM 2



void produce(){
    //production
}

void consume(){
    //consumption
}

int main() {
    std::cout << "Hello, World!" << std::endl;
    int i,j;
        std::thread producer(produce);
        std::thread consumer (consume);
    return 0;
}

代码本身没有任何内容

编辑 在线程库中有

#pragma GCC system_header

#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else

#include <chrono>
#include <functional>
#include <memory>
#include <cerrno>
#include <bits/functexcept.h>
#include <bits/functional_hash.h>
#include <bits/gthr.h>

#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

  /**
   * @defgroup threads Threads
   * @ingroup concurrency
   *
   * Classes for thread support.
   * @{
   */

  /// thread
  class thread
  {
  public:
    // Abstract base class for types that wrap arbitrary functors to be
    // invoked in the new thread of execution.
    struct _State
    {
      virtual ~_State();
      virtual void _M_run() = 0;
    };

你能确定这个库在 CLion 工具链中是否可用吗?例如 Cygwin 确实有 include.

CLion 在无法使用库 link 编码时显示为红色。 这可能是主机环境变量错误。确保您的 CMakeLists.txt 正常工作并且您的环境变量、标准库 linkage 以及您的编译器设置正确。 编译器版本和标准库兼容。 (例如,您正在使用交叉编译器(RasPi,Android),但环境变量显示主机库等将导致它失败)

检查这个相关的 post,它可能会有所帮助。

C++11 std::threads vs posix threads

好的,所以我终于解决了这个问题。我安装了 Cygwin 并在 CLion 设置中手动链接了 C/C++ 编译器(出于某种原因 CLion 无法自动检测它们)。清除所有并重新索引项目。现在它没有显示任何错误并且代码可以编译。

关于 MinGW,我在 cplusplus.com some posts regarding the issue but they were about previous versions of MinGW and it was said that they finally fixed it, however I tell: No, they didn't. Here 上读到有一个很好的存储库,它的 README 文件表明 win32 的线程依赖于 gthreads,但是我在我的库中找到了 gthread 文件,一切似乎都正常......所以仍然需要调查这个问题。如果你知道更多,在这里写下你的想法和经验。

目前解决方案是Cygwin,用它代替MinGW。

P.S。感谢 @KillzoneKid 提供链接