3

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

Here is the code:

#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()
{
    int min = 0;
    int max = 3;
    int numThreads = 1;

    std::thread* ths[numThreads];
    int threadCount[numThreads];

    int minThread = 0;
    int maxThread = 0;
    int formerMax = 0;

    for (int i = 0; i < numThreads; i++)
{
    if (i == 0)
    {
        minThread = min;
        maxThread = min + (max - min)/numThreads;
        formerMax = maxThread;
    }
    else
    {
        minThread = formerMax + 1;
        maxThread = minThread + (max - min)/numThreads;
        formerMax = maxThread;
    }

    if (maxThread > max)
    {
        maxThread = max;
    }

    std::thread* th = new std::thread(myRun, minThread, maxThread, threadCount, i);
    ths[i] = th;
}
}


void myRun(int min, int max, int* threads, int index)
{
    threads[index] = primesInRange(min, max);
}
5
  • sometimes you have to explicitly spec the templated function using <>. I also wonder if the compiler would pick it up if you cast the args... Commented May 13, 2021 at 4:58
  • This looks OK to me and compiles in gcc - which compiler (including version) are you using? Commented May 13, 2021 at 4:58
  • Non reproducible ideone.com/FdNCcH please edit your question to contain minimal reproducible example Commented May 13, 2021 at 5:14
  • @KenY-N MinGW gcc 8.1.0 Commented May 13, 2021 at 5:39
  • @Slava I've updated my post, sorry about that. Commented May 13, 2021 at 5:42

2 Answers 2

2

If you follow error messages further compiler tells you exactly what is the problem:

prog.cpp:41:82: note: variable-sized array type ‘int (&)[numThreads]’ is not a valid template argument std::thread* th = new std::thread(myRun, minThread, maxThread, threadCount, i);

Note VLA is not allowed in C++, use std::vector instead, you can still pass it's data through pointer to int

std::vector<std::thread> ths( numThreads );
std::vector<int> threadCount( numThreads );

....
ths[i] = std::thread(myRun, minThread, maxThread, threadCount.data(), i);

but it would be cleaner to pass reference to int instead:

void myRun(int min, int max, int &count );

then later:

ths[i] = std::thread(myRun, minThread, maxThread, std::ref( threadCount[i] ) );
Sign up to request clarification or add additional context in comments.

Comments

0
int numThreads = 1;

std::thread* ths[numThreads];
int threadCount[numThreads];

This is not legal C++ code. C++ does not have variable-length arrays. Some compilers offer them as an extension, but if you use them, you are basically on your own. Their interaction with the rest of the language is not very well documented. Here, you have template argument deduction not working as desired.

Do not use variable-length arrays in C++, use std::vector instead.

On an unrelated note, you should not need to new a thread object or use a pointer to std::thread. Try std::vector<std::thread>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.