I have a program that presents the user with a menu and lets the user select various actions. The program deals with passwords and usernames so i'm trying to make it as secure as possible. What I want is for the program to timeout and shut down after so many minutes. As of right now I can make it do that but only when someone interacts with the program after that time has passed. The code is below:
So what this does is starts the timer before it asks the user to pick a menu option and then once the user has selected an option it checks the time to see if it's been 5 minutes and if so shuts down.
What I need it to do is check to see if it's been 5 minutes regardless of whether the user selects an option. I'm not sure how to get around this since the program holds up the getch() function.
you need threads, but getch will still hold up a thread and though you can terminate a thread by force its not recomended...
I think boost has something with threads, google boost threads
You don't say what platform this is for, so I'm going to assume POSIX. You can do this will poll(). This is quite low-level compared to just using std::cin. You will need to call poll() on STDIN_FILENO with a 5 minute timeout. poll() will return when either there is either data to read (at which point you can read from std::cin) or it has timed out.
"simply" start 2 threads... one for the input and the other for the timer, which - when expired - will initialize clean-up and shuts the programm down...
Ok i've never used threads but i've done some reading up on them. It sounds like the POSIX library (pthread.h) is the most common but is that compatible only with Linux machines? If it is compatible with Windows machines, is it really the best course to take or are there other, simpler, and more efficient alternatives?
Alright, so i checked out binaryboy's idea and honestly i dont' have the slightest clue how to make that work...i'm pretty sure it's beyond my skill level. That said, i'm back to looking at threads as my solution. Problem is I can't seem to find a decent example. Does anyone happen to know of any?