Hello Everybody
I am getting the user input using "stdin" from a seperate thread running in application "ABC". The problem I am facing is that when the application "ABC" is finished and user didnt enter any input than a deadlock is created between stdin and fflush. because "stdin" is still running in the thread and "fflush" is called by OS when it is terminating the program even I am terminating the threads explicitly.
Any body have any idea that how can i remove deadlock.
Secondly How can I get the control back from "stdin" or how can i call "stdin" in non blocking mode.
Thanx in advance for your help and suggestions
Best Regards
Moin

threads and standard Input (stdin or cin)
luoo
Hi
I am creating a thread in my application and calling the scanf("%s", msg ) in this thread. and this thread is waiting for input from the user.
My application is now preforming some other tasks. e.g.
main( ){
CreateThread( &hThread, UserCreatedthread ).
....
performing some tasks
...
KillThread(hThread);
return 0;
}
UserCreatedthread ( ... )
{
scanf("%s", msg);
}
after the statement (return 0;) the control is moved to OS. and OS is calling fflush for all streams (stdin, stdout,stderr). but the stdin is locked by scanf() in the child thread. even i am killing the thread explicitly the stdin in locked. Due to this lock the fflush is not returning back.
Now my question is that how can I unlock this input stream.
Mick_RW
Hello,
The safest and most recommended way of terminating a thread is that the thread function returns. All other ways are not recommended.
There can be mechanisms to unlock the stream, but again this can introduce some other bugs in your application.
Why don’t you look into functions other than scanf to read input. Conio.h has the console input/output functions. One possible function you could use is _kbhit()
http://msdn.microsoft.com/library/default.asp url=/library/en-us/vccore98/HTML/_crt__outp.2c_._outpw.2c_._outpd.asp
You can combine some timeout logic along with kbhit() to prevent the problem of indefinite wait in case of no user input.
Thanks
Sarita Bafna
Visual C++ team.
Neilgd
Could you post a brief example illustrating your problem (likely a main, a thread being spawned and calling cin, the main terminating the thread calling cin and then exiting). That will help us be more explicit.
Thanks,
Ben