无法解析命名空间成员"线程"

Can't resolve namespace member 'thread'

本文关键字:成员 线程 命名空间      更新时间:2023-10-16

我想用标准的C 线程而不是UNIX练习,但是很快就会遇到问题,每当我编写 std::thread clion用红色强调它并说无法解析名称空间'thread'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确实具有包括。

clion在无法将码链接到库时显示红色的事物。它可能是主机环境变量错误。确保您的CMakeLists.txt正在工作,并且环境变量,标准库链接以及编译器设置正确。编译器版本和标准库兼容。(例如,您正在使用跨编译器(RASPI,Android(,但环境VARS显示主机库等将使它失败(

(

检查此相关帖子,可能会有所帮助。

c 11 std ::螺纹vs posix threads

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

关于mingw,我在cplusplus.com上阅读了一些有关此问题的文章,但它们是关于mingw的先前版本,据说他们终于修复了它,但是我告诉我:不,他们没有。这里有一个不错的存储库,其读数文件表明Win32的线程依赖于Gthreads,但是我在库中发现了Gthread文件,一切似乎都还好...因此仍然需要调查问题。如果您了解更多,请在这里写下您的想法和经验。

目前解决方案是cygwin,使用它代替mingw。

P.S。感谢@killzonekid链接