Anyone would know why the following code not running as expected. It is quite intermittent; sometimes you would get the message print and other times you won't. Not sure what i am missing but the code looks legit and i am not sure what may cause the message "re-setting timer value . . .\n" to be printed so randomly. Thanks.
using namespace std;
const int MAX = 5;
There's no guarantee that your comparison will check for every increment of the timer. The increment() thread might run twice or more before you actually check the comparison in the counter() thread. So you could very well skip over the max.