【发布时间】:2018-10-31 15:14:41
【问题描述】:
我正在尝试用 C++ 进行生产者/消费者设计,但实际上我被卡住了。 我收到了这个错误
错误:调用不匹配
'(std::thread) (void (&)())' prods[i]>(producer,i);
当我从循环中调用生产者函数时 这是我的主要内容:
int main(int argc, char* argv[])
{
std::thread prods[argv[1]];
for (int i = 0; i <= argc;i++){
prods[i](producer,i);
}
return 0;
}
这里是我的生产者功能
void producer(int i){
std::unique_lock<std::mutex> lock(m);
std::cout << "(1) produit 20";
jobs.push(20);
notified = true;
cond_var.notify_one();
done = true;
}
有人知道如何解决这个问题吗?提前致谢。
【问题讨论】:
-
我刚刚编辑了帖子,生产者线程的数量将作为主要参数给出
-
std::thread prods[argv[1]];不是标准 C++(VLA 是编译器扩展),您也不能使用char*作为大小。使用std::vector<std::thread> -
同样的错误:没有匹配调用:
‘(__gnu_cxx::__alloc_traits<std::allocator<std::thread> >::value_type {aka std::thread}) (void (&)())’
标签: c++ multithreading