【发布时间】:2018-02-06 12:17:33
【问题描述】:
我想用标准的 C++ 线程而不是 UNIX 线程来练习,但很快就遇到了一个问题,每当我写 std::thread CLion 时都会用红色下划线并说 Can't resolve namespace member '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 下划线 不相关。它编译了吗?
-
你得到什么错误信息?
-
OT:
#define BUFFER_SIZE 3不要定义整数常量。使用const int。 -
这段代码是错误的,因为你既不能
join也不能detachjoinablethread销毁之前的对象。 -
@DanielLangr 你是对的,但我确信错误不是因为缺少
join或detach
标签: c++ multithreading c++11 clion mingw-w64