Question: To create a program which takes user input but times out after some seconds (say it's 2 seconds for now).
Approach: I created two threads, one to wait for user input (inputThread
with tid[0]
) and other to sleep for 2 seconds (sleepThread
with tid[1]
). I cancel a thread from another thread's routine, as follows:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
pthread_t tid[2];
void* inputThread()
{
int n;
printf("Enter number:");
// wait for input; if input is given then go ahead and cancel the sleeper thread.
scanf("%d",&n);
// cancel the sleeper thread
pthread_cancel(tid[1]);
printf("Got value:%d\n",n);
}
void* sleepThread()
{
// sleep for 2 seconds and cancel the input thread.
sleep(2);
// cancel the input thread
pthread_cancel(tid[0]);
printf("\nNo value entered!\n");
}
int main(int argc, char const *argv[])
{
int r1,r2,r3,r4;
// input taking thread
r1 = pthread_create(&tid[0],NULL,inputThread,NULL);
// sleeping thread
r2 = pthread_create(&tid[1],NULL,sleepThread,NULL);
r3 = pthread_join(tid[0],NULL);
r4 = pthread_join(tid[1],NULL);
return 0;
}
As of now, the program works as expected.
But my friend says that it's not guaranteed to work since it depends on how threads are scheduled. He tried explaining the same to me but I couldn't understand. He also said that pthread_cancel
is only a request to cancel the threads and it may not succeed.
So can someone please point out the potential mistake and best practice to avoid the same. Any changes to be made to guarantee the program's working is also appreciated.