I am writing in Visual Studio 2013. There is, therefore, a function that should be executed in the stream:
void click(int &a, int &flag){ do{ a = _getch(); } while (flag != 0); } And there is a main function in which a thread is declared (thread class) and when you click on the keyboard, the button code is displayed.
int keyState = 0; void main() { int fl = 1; thread input(click, ref(keyState), ref(fl)); while (true){ while (keyState == 0){} cout << keyState << endl; keyState = 0; } system("pause"); }
While I collect in Debug, then everything goes fine, but it is worthwhile to build the project in Release, as the whole program does not work. The reason in line
while (keyState == 0){} It is worth adding any output to the loop body.
while (keyState == 0){ cout << ""; } how everything starts to work. Me as a beginner (2nd year uni), it just leads to a stupor. What is the reason?