为什么我不能编译这个简单的线程测试?
Why can't I compile this simple thread test?
我想在我的Macbook Pro上用线程测试一些东西,但我无法让它工作。
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
这是我的机器上安装的 clang 版本。我尝试编写一些线程向量,但这不起作用,所以我回去从 SO 复制了一个示例。
#include <string>
#include <iostream>
#include <thread>
using namespace std;
// The function we want to execute on the new thread.
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");
// Do other things...
// Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
t1.join();
}
但是我收到编译器错误。
error: no matching constructor for initialization of
'std::__1::thread'
thread t1(task1, "Hello");
我想我的机器是问题所在,但为什么呢?
不知何故,您将代码构建为 C++03,可能是因为没有显式提供标准修订标志。 libc++,标准库的LLVM实现允许在C++03代码中使用<thread>
。源具有以下类型的条件编译:
#ifndef _LIBCPP_CXX03_LANG
template <class _Fp, class ..._Args,
class = typename enable_if
<
!is_same<typename __uncvref<_Fp>::type, thread>::value
>::type
>
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
explicit thread(_Fp&& __f, _Args&&... __args);
#else // _LIBCPP_CXX03_LANG
template <class _Fp>
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
explicit thread(_Fp __f);
#endif
在 C++11 及更高版本中,构造函数遵循 C++11 标准。否则,它只接受可调用对象,而不接受其他参数。我设法通过提供 C++03 标准修订标志重现您的错误。该错误甚至提到了这个候选人:
prog.cc:16:12: error: no matching constructor for initialization of 'std::__1::thread'
thread t1(task1, "Hello");
^ ~~~~~~~~~~~~~~
/opt/wandbox/clang-8.0.0/include/c++/v1/thread:408:9: note: candidate constructor template not viable: requires single argument '__f', but 2 arguments were provided
thread::thread(_Fp __f)
相关文章:
- MS 本机单元测试 - 断言::线程失败不起作用
- 使用 std::chrono::steady_clock 对线程/异步中的代码进行基准测试
- 为什么我不能编译这个简单的线程测试?
- 线程池的计时测试:单线程 vs 回调 tp vs 未来 tp
- 测试队列中等待condition_variables的多个线程
- 如何在Visual Studio中设置C /C代码仅在一个线程中运行以测试运行时间
- 对线程性能进行基准测试
- 提升线程链接在 Netbeans 7.1 调试/测试会话中失败
- 如何使用谷歌测试与线程
- 生产者和使用者函数,用于在操作手册中测试C++并发的线程安全堆栈示例
- 对多线程输入的QTableWidget进行基准测试,结果很奇怪
- Boost单元测试可以是多线程的吗
- 为什么我不能在 Boost.Test 测试用例中创建线程?
- 有没有什么方法可以测试C++API(在ubuntu-linux上)是否是重新集中的(线程安全的)
- 在自动测试后安全地清理阻塞的std::线程
- PC多线程性能和稳定性问题的简单基准测试.如何使每个线程在单独的核心上运行
- 测试静态局部对象的初始化是否线程安全
- 如何测试一个std::线程是否从
- c++线程速度测试,我做得对吗?
- 线程池性能测试