简体   繁体   中英

Problem with thread function arguments in C++

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM