Home > Software design >  How can I fix C error C2672 in my code with threads?
How can I fix C error C2672 in my code with threads?

Time:11-14

When I'm trying to compile program with threads, C2672 error ('invoke': no matching overloaded function found) is occuring.

My code:

#include <iostream>
#include <thread>
// the procedure for array 2 filling
void bar(int sum2)
{
    for (int i = 100000; i < 200000; i  ) {
        sum2 = sum2   (i   1);
        std::cout << sum2;
    }
}
int main()
{
    // the second counter initialization
    int sum2 = 0;
    // the constructor for the thread
    std::cout << "staring thread2...\n";
    std::thread thread(bar);
    // the thread detaching
    thread.detach();
    // the first counter initialization
    int sum1 = 0;
    // filling of the first array
    for (int i = 0; i < 100000; i  ) {
        sum1 = sum1   (i   1);
        // elements output
        std::cout << sum1;
    }
}

Tell me please, how to fix this bug?

CodePudding user response:

You need to pass a value to the thread you're creating,

std::thread thread(bar, N);

where N is the integer value, as you've defined in your void bar(int) function.

#include <iostream>
#include <thread>

// the procedure for array 2 filling
void bar(int sum2)
{
    for (int i = 100000; i < 200000; i  ) {
        sum2 = sum2   (i   1);
        std::cout << sum2;
    }
}

int main()
{
    // the second counter initialization
    int sum2 = 0;
    // the constructor for the thread
    std::cout << "staring thread2...\n";
    std::thread thread(bar, 100);
    // the thread detaching
    thread.detach();
    // the first counter initialization
    int sum1 = 0;
    // filling of the first array
    for (int i = 0; i < 100000; i  ) {
        sum1 = sum1   (i   1);
        // elements output
        std::cout << sum1;
    }
    return 0;
}

CodePudding user response:

The signature of OPs function bar() is:

void bar(int sum2)

Hence, when the function is passed to std::thread, an argument is required to initialize bar() functions parameter int sum2.

When std::thread tries to invoke bar() internally, it does so without any argument but there is no overload of bar() without an argument. Hence, the error

C2672 error ('invoke': no matching overloaded function found)

Instead of std::thread thread(bar); it should be std::thread thread(bar, sum2); or std::thread thread(bar, 0); or something similar.

The fixed example of OP (with some other minor adjustments):

#include <iostream>
#include <sstream>
#include <thread>

// the procedure for array 2 filling
void bar(int sum2)
{
    for (int i = 10/*0000*/; i < 20/*0000*/; i  ) {
        sum2 = sum2   (i   1);
        std::cout << (std::ostringstream() << " 2: " << sum2).str();
    }
}
int main()
{
    // the second counter initialization
    //int sum2 = 0; // UNUSED
    // the constructor for the thread
    std::cout << "staring thread2...\n";
    std::thread thread(bar, 0);
    // the thread detaching
    thread.detach();
    // the first counter initialization
    int sum1 = 0;
    // filling of the first array
    for (int i = 0; i < 10/*0000*/; i  ) {
        sum1 = sum1   (i   1);
        // elements output
        std::cout << (std::ostringstream() << " 1: " << sum1).str();
    }
}

Output:

staring thread2...
 1: 1 1: 3 1: 6 1: 10 1: 15 1: 21 1: 28 1: 36 1: 45 1: 55 2: 11 2: 23 2: 36 2: 50 2: 65 2: 81 2: 98 2: 116 2: 135 2: 155

Live demo on coliru

  • Related