【问题标题】:std::thread issues with compiling [closed]std::thread 编译问题[关闭]
【发布时间】:2021-08-03 09:27:04
【问题描述】:

这个主函数的目标是找到一个范围内的素数数量,使用线程将问题划分为选定数量的线程。我遇到了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);
}

【问题讨论】:

  • 如果你把线程应该解决的所有问题都剪掉,我想有人可以帮你解决线程部分。请创建minimal reproducible example 以显示线程之间传递信息的问题。

标签: c++ multithreading pthreads std stdthread


【解决方案1】:

minmax 在您调用此行时是未定义的变量名称:

 std::thread* th = new std::thread(myRun, min, max, threadCount, i);

无论如何,将变量命名为minmax 绝不是一个好主意。经常与std::minstd::max 混淆,并且许多代码库都有类似命名的宏。

【讨论】:

  • 我更改了变量名,但仍然出现上述错误
  • 您的代码编译良好 - 但是,您缺少对 primesInRange 的定义。所以链接会失败。
猜你喜欢
  • 2013-11-26
  • 2012-05-23
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多