Referenced argument not updating

I have a simple program that outputs two strings. The program is supposed to output the second string once the user presses the enter key. Just in case the user doesn't press the enter key, I have a second thread running, that will wait for a small amount of time, and check if the user has pressed enter or not, and instruct the user to do so in case he hasn't:

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
27
28
29
30
31
32
33
34
35
36
37
void wait(int time)
{
     std::this_thread::sleep_for(std::chrono::milliseconds(time));
}

void pressEnter(int& q)
{
     //wait for 10 seconds
     wait(10000);
     //If the user hasn't pressed enter, then q will be 0
     if (q == 0)
     {
          std::cout << std::endl;
          std::cout << "(try pressing the Enter key)";
     }
}

int main()
{
     std::string intro[2] = {"Welcome to this program", "Hope you enjoy it"};
     int q = 0;

     std::thread enterCheck(pressEnter, q);

     for (q; q < 2; q++)
     {
          std::cout << intro[q];
          //Wait for the user to press enter
          std::cin.get()
          //Clear the buffer
          std::cin.sync();
     }
     enterCheck.join();

     system("pause");
     return 0;
}


What I expected to happen:
It is to my understanding that when you pass an argument by reference (int& q), should you change the value of q in main (or anywhere else for that matter), the reference means those changes will be noted by any pointer/references.
So when in main, the user presses enter, the value of q changes to 1, and when the enterCheck thread comes to comparing the value of q, it should find that the value is 1, and not output the request for the user to press the enter key.

What actually happens:
When the thread starts, q in the thread maintains the value it had when the thread started, and the value doesn't change according to what happens elsewhere in the program, and the program will output "(try pressing the Enter key)" after 10 seconds, regardless of whether q is equal to 1 or not.

What I request:
I ask that if somebody could help me and explain to me why the value of q in the thread isn't the same as q in main, I'd be extremely greatful. Also, if a solution is provided as to how I can get my achieved result, that'd also be extremely helpful.

Thank you in advance.
You need to pass q by reference:

std::thread enterCheck(pressEnter, std::ref(q));

Also, you may want to look into condition variables instead.
http://www.cplusplus.com/reference/condition_variable/condition_variable/
http://en.cppreference.com/w/cpp/thread/condition_variable
Last edited on
Such a simple yet effective solution! Should this be done with just threads, or can it be used with normal function calls aswell?

Thank you!
You only need to use std::ref or std::cref when passing arguments to functions that forward the arguments again (e.g. std::function) - for normal function calls references are bound automatically (and in fact std::ref wouldn't even work then).
Last edited on
Topic archived. No new replies allowed.