Duplicate of Is it possible to set timeout for std::cin?
Asked
Active
Viewed 171 times
0
-
In short you cannot with `cin`. Dealing with input in a less abstract way than "a byte stream" requires using other tools. In posix world, you would use `select` or a similar mechanism, in windows world GetMessage or something similar. – spectras Apr 22 '20 at 12:39
-
[just use alarm](https://stackoverflow.com/questions/15524122/how-to-implement-timeout-for-getline) – Katsu Apr 22 '20 at 12:40
-
`alarm` is not portable (windows, looking at you), and there is no guarantee it will actually interrupt cin (though it does in all platforms implementing it that I know of). – spectras Apr 22 '20 at 12:46
-
Does this answer your question? [Is it possible to set timeout for std::cin?](https://stackoverflow.com/questions/9053175/is-it-possible-to-set-timeout-for-stdcin) – spectras Apr 22 '20 at 13:02
-
@spectras Do you know why it is not possible? What is the underlying reason? – AnonymousUser3312142 Apr 22 '20 at 16:56
-
Your program only finishes once all threads are done. So your main thread completes at timeout. Then the program waits for your cin thread to complete before finishing. – spectras Apr 23 '20 at 10:09