Taking input while in some form of waiting/sleeping -- plz help
-
The subject basically says the gist of it. I'm trying to write a program in C++ where it waits for input for up to 2 seconds but if nothing happens in 2 seconds, it stops looking for input and goes off to do something else. I know this may touch on stuff that you need multi-threading for but I think I remember reading somewhere that there's a simpler way for something as simple as I'm looking to do. Does anyone have any code snippets or any ideas? Thanks in advance for your help. MaKuTu t3h n00b
-
The subject basically says the gist of it. I'm trying to write a program in C++ where it waits for input for up to 2 seconds but if nothing happens in 2 seconds, it stops looking for input and goes off to do something else. I know this may touch on stuff that you need multi-threading for but I think I remember reading somewhere that there's a simpler way for something as simple as I'm looking to do. Does anyone have any code snippets or any ideas? Thanks in advance for your help. MaKuTu t3h n00b
Probably more elegant to do this in a multithreaded fashion, but yes, it's possible to do what you want without multithreading. First, you're going to need non-blocking input. If you don't have that, I think you're pretty much SOL. Assuming you have non-blocking input, the basic logic is:
int granularity = 100; // number of milliseconds to sleep between attempts
int attempt = 2000; // number of milliseconds to try
int attempted = 0; // counter
while (attempted < attempt)
{
// check for input (must not block)
if (inputIsAvailable())
{
// we already have input
doSomething();
}
else
{
// wait for input
Sleep(granularity);
attempted += granularity;
}
}
// we never got input
doSomethingElse(); -
Probably more elegant to do this in a multithreaded fashion, but yes, it's possible to do what you want without multithreading. First, you're going to need non-blocking input. If you don't have that, I think you're pretty much SOL. Assuming you have non-blocking input, the basic logic is:
int granularity = 100; // number of milliseconds to sleep between attempts
int attempt = 2000; // number of milliseconds to try
int attempted = 0; // counter
while (attempted < attempt)
{
// check for input (must not block)
if (inputIsAvailable())
{
// we already have input
doSomething();
}
else
{
// wait for input
Sleep(granularity);
attempted += granularity;
}
}
// we never got input
doSomethingElse(); -
I guess I don't know whether or not I have non-blocking input. Do you know any examples of what a function like that might be called/what header file you need? Thx
Not really. That would be entirely dependent on what libraries you ae using for input. Perhaps whatever library you are using provides a function (with a name such as bytes_available()) which returns the number of bytes currently available for access. I've used libraries (e.g. JNetLib) which has this type of functionality, but many libraries do not. It is possible to wrap an asynchronous APi and achieve this kind of functionality (which is what JNetLib does), but this wrapping will require threading. It really depends on what you are trying to do, where this input is coming from, and what libraries you are using.