How do I safely make a thread with a loop that awaits input?

Hey there, im making my first c++ based fullstack app and I have a problem where I need to wait for the player to hit enter asynchronously

The way I have it set up is in two classes: The app class that gets initialized in main and the Input class which gets initialized theoretically whenever I need it. After the init app function I have a cout in main that states that Im back in the main.cpp but it never gets called

But for testing purposes ive made a testinit() and testcallback() within my app class to test functionality and it looks something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void TestInit()
{
  Input* input = new Input();
  input->InitInput(this, &App::TestCallback)

  do
  {

  }while(!ejected); // this would be the main program loop, but for now i need  
  to verify that the input system works

  cout << "Out of loop!" 
}

void TestCallback()
{
  cout << "callback worked UwU";
  callbackbool = true; //global bool
}


and in the input class it kind of looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
void InitInput(App* app, void(App::*Callback)())
{
  ejected = false; //class bool thats used for detecting if player pressed enter
  
  if(app)
  {
     application = app;
     CallbackFunc = Callback; //class varriables self explainitory 
  }

  std::thread t6(&Input::TakeInput, this);

}


and the TakeInput:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void TakeInput()
{

  char keyPressed;

  do
  {
     if (_kbhit())
     {
         keyPressed = _getch();
         
         if(keyPressed == '\r')// yes its a terminal program leave me alone
          {
               ejected = true;
               (Application->*CallbackFunc)();
          }
     }

  }while(!ejected);
}


And all of this works just fine. I get the output of the call back and all is good in the world but it throws me an error right at the end of the Init function for the app and I dont have the slightest clue as to why. Any help appriciated.

The error it gives me is something along the lines of ESP not properly copied or something
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <thread>
#include <functional>
#include <chrono>

void wait_for_enter_and_call_back( std::function< void() > call_back )
{
    std::cout << "press enter: " << std::flush && std::cin.get() ;
    call_back() ;
}

int main()
{
    std::atomic<bool> enter_detected { false } ;
    // detach(): create a detached which runs in the background (we won't need to call join on it)
    std::thread( &wait_for_enter_and_call_back,
                 [&] { enter_detected = true ; std::cout << "enter detected\n" << std::flush ; } ).detach() ;

    std::this_thread::sleep_for( std::chrono::milliseconds(10) ) ;
    std::cout << "\nwaiting for enter before quitting main " << std::flush ;
    while( !enter_detected ) // wait for enter
    {
        std::cout << '.' << std::flush ;
        std::this_thread::sleep_for( std::chrono::milliseconds(100) ) ; // check after every 100 ms
    }
}
Topic archived. No new replies allowed.