I have currently 2 pthreads running and I would like to wait for one of them to end for my program to continue.
In my pthreads I have a variable which can be true or false (it is a global variable). After creating the thread (one asking for a input in a cin and one waiting 10 seconds and if it reach 10 sec it kill the "cin" thread and end itself, the "cin" thread kill the "timer" thread if there is cin detected) I would like for my program to wait. When each of the thread end they put a variable "stoptimer" to true.
First after creating the threads I started writing a while loop like this :
while(stoptimer==false){}
The threads start, we enter into the while loop but even when the threads end and "stoptimer" goes to true we don't exit the loop.
I am currently doing this :
rc = pthread_create(&threads[1], NULL, Timer, (void *)&td[1]);
if (rc) {
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
rc = pthread_create(&threads[2], NULL, Choix, (void *)&i);
if (rc) {
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
while (stoptimer==false) {
cout<<"wait"<<endl;
}
Here you can see I am creating the threads and then entering the while loop with something in it. If I keep this loop as it is it is doing what I want, when the timer end or the user input a value we quit the while because "stoptimer" is no more false. But I don't want this cout to be here.
I tried putting a comment inside the loop in order for it to not be empty but it still react as being empty. In my understanding if it worked with the count then now it should work but simply do nothing.
Why is it doing this? Is there something special about empty while loop?