【发布时间】:2015-06-23 07:26:46
【问题描述】:
我有一个包括线程库的问题。以下代码:
#include <string>
#include <iostream>
#include <thread>
using namespace std;
//The function we want to make the thread run.
void task1(string msg)
{
cout << "task1 says: " << msg;
}
int main()
{
// Constructs the new thread and runs it. Does not block execution.
thread t1(task1, "Hello");
//Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
t1.join();
}
产生这些错误:
代码取自另一个 stackoverflow 问题的答案。我对代码块和 C++ 还很陌生,所以请向我解释我做错了什么。
【问题讨论】:
-
好吧,确实很奇怪。我可能想知道的:你能找到标题
thread本身,然后看看那里吗?它会在 GNU/Linux 中的路径/usr/include/c++/SomeVersion/thread中,并且我猜在 Windows® 中的…/include/c++/SomeVersion/thread中的某个位置(在后一种情况下,您需要稍微搜索一下)。您必须查找该文件中某处是否有单词class thread。根据您的问题,我猜测标题以某种方式搞砸了。 -
@Hi-Angel:看起来带有 Code::Blocks 的开箱即用的 TDM 编译器是在不支持线程的情况下编译的。您必须下载不同的编译器并将其与 Code::Blocks 一起使用才能获得线程支持。
-
如果你使用 mingw-w64 和 win32 线程,那么你需要使用meganz threading addon
标签: c++ multithreading codeblocks