1

I know this question is a duplicate, however I could not find an answer on the other questions that helped my particular case. I should mention I am quite new to C++ and may be making a variety of mistakes that have nothing to do with the thread module. My goal here is just to have a lamda expression be run in a thread constantly, this lambda function would add one to a value every time spacebar is pressed. The program then checks this value to see if it is greater than 10, if it is, the program exits. The problem is that every time I try to build it (Using Visual Studio Community 2019), I get the following error:

'std::invoke': no matching overloaded function found

Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'

Here is my code:

#include <thread>
#include <iostream>
#include <Windows.h>

using std::thread;
using std::cout;
using std::endl;

int main()
{
    //Value modified by the lambda expression, when it becomes greater than 10,
    //the program will wait for the thread to finish and then exit.
    int number = 0;

    //Value to be checked by the lambda expression everytime the loop repeats,
    //if it is false, the loop will break.
    bool programRunning = true;

    //Lambda expression to update number on screen when spacebar is pressed.
    auto update = [](bool& isRunning, int& number) {
        while (isRunning)
        {
            //If space is pressed
            if (GetAsyncKeyState(VK_SPACE) != 0)
            {
                //Add one to the number, this is what the program checks to see if it should exit.
                number++;
                system("cls");
            }
            cout << "The current number is " << number << endl;
            Sleep(500);
        }
    };

    //Creating thread from lambda function and passing references of programRunning and number.
    thread Updater(update, &programRunning, &number);

    //Checks if the number modified by the lambda expression is greater than 10,
    //if so, the program waits for the thread to finish and then exits.
    while (true)
    {
        if (number > 10)
        {
            programRunning = false;
            Updater.join();
            exit(0);
        }
        else
        {
            Sleep(500);
        }
    }
}
Jackal
  • 195
  • 9
  • 1
    Your lambda `update` accepts `bool&` and `int&` as arguments. Here -> `thread Updater(update, &programRunning, &number);` you are trying to launch a new thread with that `update` lambda (which fortunately doesn't capture anything so it can decay to a function pointer), a `bool*` and an `int*`. Try passing reference wrappers instead of those pointers. – Big Temp May 18 '20 at 05:04
  • 1
    There are multiple problems here, aside from the compilation error. Such as a complete lack of sequencing, resulting in undefined behavior. Mutexes and condition variables are required to properly sequence inter-thread communication, like this. I doubt, very much, that anything like this results from following a methodical guided study with a C++ textbook. It is not feasible to try to learn C++ by cobbling together things one might find, in bits and pieces, from random Google searches. C++ is the most complicated general purpose programming language in use today. You need a good C++ textbook. – Sam Varshavchik May 18 '20 at 05:05
  • @BigTemp Thanks so much! Still getting used to the syntax, and like SamVarshavshik said my learning technique is not ideal. I think I tried to apply the same technique I used to learn Python in order to learn C++, which is a terrible idea in hindsight. – Jackal May 18 '20 at 05:09
  • 1
    @Jackal Learning idiomatic and efficient C++ is very hard and time-consuming so yes, as someone who still tries to learn it without any guidance of a particular book or other resource I'm going to agree with Sam here - you'd be better off picking up something from [that](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list if you don't want to fill your search history with a few gigabytes of "Should I do X in C++" type of questions. – Big Temp May 18 '20 at 05:28
  • 1
    Learn about _data races_, which cause _undefined behavior_. You need some kind of synchronization when accessing data from multiple threads (if there is writing involved). Such as _atomic variables_ in your case (`programRunning` and `number`). Also, take a look at `std::reference_wrapper` and `std::ref`, or capture shared variables by reference. – Daniel Langr May 18 '20 at 06:25

0 Answers0