c++ - 在简单代码演示中使用 std::thread 时有关重载函数的 C++ 编译错误
问题描述
我正在使用 std::thread 编写以下演示:
#include <thread>
#include<mutex>
#include<vector>
class ts {
public:
ts() {};
~ts() {};
void operator() (int x) {
mu.lock();
std::cout << "i'd like to output " << x << ", thanks\n";
mu.unlock();
}
std::mutex mu;
};
int main()
{
ts t;
std::vector<std::thread> v;
for (int i = 0; i < 4; i++)
v.push_back(std::thread(t,i));
for (int i = 0; i < 4; i++)
v[i].join();
return 0;
}
编译时,我得到一个错误:
memory(3378): error C2661: 'std::tuple<ts,int>::tuple': no overloaded function takes 2 arguments
thread(67): note: see reference to function template instantiation 'std::unique_ptr<_Tuple,std::default_delete<_Tuple>> std::make_unique<_Tuple,ts&,int&,0>(ts &,int &)' being compiled
thread(89): note: see reference to function template instantiation 'void std::thread::_Start<ts&,int&>(_Fn,int &)' being compiled
with
[
_Fn=ts &
]
<source>(22): note: see reference to function template instantiation 'std::thread::thread<ts&,int&,0>(_Fn,int &)' being compiled
with
[
_Fn=ts &
]
解决方案
推荐阅读
- javascript - 将 html 文件作为 innerHTML 的值传递
- javascript - 为什么 iPhone iOS 显示 momentjs 的无效日期
- apache-beam - 如何从 Apache Beam 中的 HTTP 响应中读取大文件?
- r - 在R中计算带有条件的运行总计
- c++ - 当vector是tje全局变量时,c ++中vector的内存管理
- jar - 无法下载 builder.jar (com.android.tools.build:builder:3.2.0):没有可用于离线模式的缓存版本
- flutter - 一个奇怪的 Scrollview 警告...在 Flutter 中构建 apk 时产生问题
- node.js - Jest 的 globalSetup 使我的测试无法被识别
- excel - 在散点图中查找特定点的参考行号
- ruby-on-rails - 无法将 rails 应用程序推送到 heroku - 编译失败