-2

The objective of this main function is to find the number of prime numbers in a range using threading to divide the problem into the selected number of threads. I'm having issues with std::thread and getting an error because of the arguments. I'm not sure of how to fix it. Any help would be greatly appreciated.

Here is the error:

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);
}

prapin
  • 6,395
  • 5
  • 26
  • 44
  • 1
    If you cut away everything your threading is supposed to solve, I think someone can help you to solve the threading part. Please make a [mre] to show the problem with passing info between threads. – Ted Lyngmo May 13 '21 at 03:31

1 Answers1

1

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.

selbie
  • 100,020
  • 15
  • 103
  • 173