c++ - std::thread 编译问题
问题描述
这个 main 函数的目标是找到一个范围内的素数数量,使用线程将问题划分为选定数量的线程。std::thread
由于这些论点,我遇到了问题并出现错误。我不确定如何解决它。任何帮助将不胜感激。
这是错误:
error: no matching function for call to 'std::thread::thread(void (&)(int, int, int*, int), int&, int&, int [numThreads], int&)' std::thread* th = new std::thread(myRun, minThread, maxThread, threadCount, i);
#include <iostream>
#include <thread>
static int isPrime(int n);
static int primesInRange(int min, int max);
void myRun(int min, int max, int* threads, int index);
int main()
{
std::thread* ths[2];
int threadCount[2];
for (int i = 0; i < 2; i++)
{
std::thread* th = new std::thread(myRun, min, max, threadCount, i);
ths[i] = th;
}
for (int i = 0; i < 2; i++)
{
ths[i]->join();
}
int result = 0;
for (int i = 0; i < 2; i++)
{
result = result + threadCount[i];
}
std::cout <<"text here\n";
}
void myRun(int min, int max, int* threads, int index)
{
threads[index] = primesInRange(min, max);
}
解决方案
min
and max
are undefined variable names when you invoke this line:
std::thread* th = new std::thread(myRun, min, max, threadCount, i);
For what it's worth, never a good idea to name a variable min
or max
. Often gets confused with std::min
, std::max
and a lot of code bases have similarly named macros.
推荐阅读
- assembly - 显示所需指令扩展的 x86 反汇编程序
- python - 带有“quad”和“args”的积分 - 杨氏双缝干涉仪
- python - youtube api 的最新视频
- c++ - C++ IDE 不会推断/自动完成模板类中 std::array 下标表达式的成员访问
- python - 张量板上显示的标量摘要不正确
- c++ - 可以使用简单队列制作优先级队列吗
- jquery - Laravel - 没有记录保存在多个记录插入中,动态创建的输入字段
- vue.js - 背景图像需要很长时间才能加载到部署在 S3 存储桶上的 VueJS
- swift - 为什么我的按钮在完成动画之前被从其超级视图中删除?
- floris - FLORIS - 改变风速/风向