3

How can i execute some commands while waiting for an input?


Something like this:

while(/*taking input*/) {
    sleep(15);
    cout<<"You still there?";

    //...     
}



So it asks for the input, but while it waits for ENTER to be pressed it executes commands.
Is it possible?


Edit:
Thanks for all the suggestions :)

16
  • You can delegate work into another thread. Commented Sep 17, 2013 at 15:09
  • 1
    multithreading. Although if you print something to console WHILE user is typing, things can get messy/confusing for the user. Commented Sep 17, 2013 at 15:09
  • As I said, I'm still a newbie. Thanks anyways, I'll figure it out :) Commented Sep 17, 2013 at 15:10
  • 11
    Multithreading is way too big a cannon for this tiny problem. A simple non-blocking I/O multiplexing loop should do just fine. Commented Sep 17, 2013 at 15:16
  • That was just an example. I want to use it for a chat, so while the 1st thread is used to send messages, the 2nd is used to receive messages :) Commented Sep 17, 2013 at 15:18

4 Answers 4

3

Using C++11 threading facilities, you can simply spawn a "worker" thread that does the work while you wait for the input.

std::async is an easy way to asynchronously get a result of an operation; your problem description isn't very precise, so I don't know what else I might add here.

It can be done without threading, but then you would need an asynchrounous access to the input, which in general isn't cross-platform.

After Martin's comment, I actually see the problem with a greater clarity; in this case, your best bet is probably to look for more abstracted IO APIs, which will provide nonblocking input, or create your own based on OS-specific API.

Sign up to request clarification or add additional context in comments.

1 Comment

No, threads don't help for this kind of work. I/O is blocking and serialised. Spawning threads won't help output stuff while waiting for input. C++ simply does not provide for this at all.
2

There are 2 ways to achieve your objective:

  1. Multithreading
  2. Asynchronous IO

For the use case you mentioned (a chat), I'd recommend to look into the second option. You may want to have a look at the documentation for boost asio, which contains an example for a chat client and server.

Comments

1

You want to spawn another thread to run in parallel with the thread's that's waiting for i/o (input from stdin in this case). Run whatever it is you want to do while waiting for i/o in that spawned thread. Have a look at Boost threads

Comments

0

If you allow to add something outside your segment, it will work:

[wolf@Targaryen]:~$ cat main.cpp 
#include <iostream>
#include <string>
using namespace std;

int main() {
  string s;
  while( getline(cin, s) and s != "" ) {
    cout << "You still there?" << endl;
  }
}
[wolf@Targaryen]:~$ r
adb
You still there?
sfcetgw
You still there?
wefew
You still there?

[wolf@Targaryen]:~$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.