Simple C++ question
I am writing a program in C++, and this program that needs to constantly be attending several tasks at specific times. Like so:
{
Input();
Task();
Output();
WaitTillSpecificTime();
}
I need to know how to implement the above without my program freezing when it comes time to get the input. If there is input, I would like to process it, else wise the program should skip to Task().
At the moment, this program is simply run on a console, during early development. I will eventually make it so that this program can be accessed by multiple remote users at once.
This is part of an ambitious long term project, which is largely undertaken to train myself to use good programming habits, and be able to show of to prospective future employers.
Its just pseudo code. Anyways, to reiterate:
In the above example, if I was to walk away from the keyboard, the program would freeze at the input section if I was using getline or cin - and this is a problem.
The above could also be phrased as such:
{
if ( ThereIsInput() ) Parse Input;
Task();
Output();
Wait()
}
I'm not sure your question makes sense. Your Input() function is already adjacent to your Task() function. So you are not going to skip it. You add another function ThereIsInput() but that seems to be serving the purpose of a flag, not a function.
Your Input() function should include the following features: check/get the IO stream, then, depending on what the results of that check/get are, you process that input. Why would you skip this Input() function? If there is no input, you can simply have your input function internal control structure return() without processing the input if there is no input data to process. To say it another way, your test for whether there is input to process should be inside the Input() function.
If Task() will always be executed, regardless of whether there is input or not, you don't need any conditionals around or before it.
Task() is time specific. It must be executed at a specific time. Waiting indefinitely for user input, as cin and getline do, is unacceptable. The fact that it will eventually run is not enough.
You seem to be saying 'all you need to do is this' in your post, and yes, I know that is what I need to do. What I need help with is how to do it. How do I check the iostream to see if there is new data without preventing the next function from being immediately called if there is no new data?
My pseudo code was presented in that way as it was the simplest way to convey that I wanted to be able to skip over the process of getting the user's commands if no new commands were available - as Douglas Adams would say, in precisely the same way that cin does not let me. It was presented like this, as simply as possible, because I am trying to convey concepts that I know are being misunderstood.
Take, for example, the following exaggerated time line.
00.00s : Task()
00.01s : Output()
00.25s : Wait() completes
00.26s : No new input, skip
00.26s : Task()
00.27s : Output()
00.50s : Wait() complete
If I was to try to do the above in the absence of human input with cin or getline, the following would happen:
00.00s : Task()
00.01s : Output()
00.25s : Wait() completes
00.26s : No new input, so the program waits here indefinitely, missing the scheduled execution of Task()
That is the problem that I am trying to solve. I know that I have to be able to check for input, what I need is how to (quasi)instantly check for the presence of new input, instead of waiting for input before proceeding.
How do you manage the timing of your execution of Task()? Are you in a loop or within another function that gets called at the right time, a function that contains the statements you listed? You have to manage the timing of Task() if it has to take place at a certain time. If you want to periodically poll for user input, you should do that whether or not Task() gets called.
I don't have a picture of what you want to do. Just what you don't want to do (don't want to wait indefinitely for user input).
You should create a flowchart to show the relationship between your modules and the steps you want your program to take.
You can find examples of flowcharts, and also code snippets of useful coding approaches to common problems at CodeProject.com. For example, this is a page on codeproject.com showing how a popular search algorithm is implemented.
Disclaimer: C++ is a bit rusty for me; haven't done it in a while.
C++ does not really have a standard facility for non-blocking keyboard input. The common way to do what I think you want to do (also considering that you'll eventually want to turn this into a daemon process for network/socket-based interaction) is to use threading, where one thread presents your main loop and a secondary thread handles the keyboard-based user-input. This will also set the initial foundation for your application to be extended into one-thread-per-user so that each remote-user session can be more or less sandboxed, so you won't run into the problem of several user's sessions messing up eachother.
However, if you're not comfortable with the threads thing yet and you just want a non-blocking keyboard read, have a look at this piece of code:
ftp://ftp.informatics.sussex.ac.uk/pub/ ... in_control
It's a wrapper around fcntl and simulated a non-blocking STDIN and might give you what you need right now. Though I'd strongly advice looking into the threading solution.
t0
Veteran

Joined: 23 Mar 2008
Age: 51
Gender: Male
Posts: 726
Location: The 4 Corners of the 4th Dimension
Yes, you want to do threads. Have one specifically set up for timed events. Then your main thread can block on input and your "timer" thread can maintain the timed events. You'll also want to ditch the "while(1)". Make it "while (!fTerminating)" or something like that so that the thread exits the loop when you're ready to shutdown.
Note that you'll also need to deal with syncronization of data if the two threads are modifying the same data.
How will multiple users access the program simultaneously? Network? If so, check out non-blocking sockets. Socket (and threading) implementation will differ based on your platform. Last I used *nix, non-blocking sockets were achieved via select(). It allows you to poll multiple sockets and read/write on the ones ready. In WinXX, better performance is achieved via IO Completion Ports.
Similar Topics | |
---|---|
question |
08 Feb 2025, 7:06 am |
Work Question |
07 Mar 2025, 12:52 am |
Grammar question |
30 Dec 2024, 7:14 pm |
I have a question for women 40 and over |
20 Feb 2025, 2:24 am |