c++ - Check for input and sleep boost thread -


I am trying to create a thread that checks the user input and if the input is equal to "exit" So it stops all the other threads.

The way I use the cin is to stop the thread. Thread should run, check user input, and if there is one and this is equal to "exit", then close run process .

This is my code that does not work, is expected that "the new line is closed" is never printed, "Running" is printed only once:

  void check_for_cin () {while (process runs) {cout & lt; & Lt; "Its going on"; String input; Std :: getline (std :: cin, input); // while (std :: getline (std :: cin, input)) {if (! Input.empty ()) {if (input == "exit") {runProcesses = false; Cout & lt; & Lt; "Out" & lt; & Lt; "Run processes:" & lt; & Lt; RunProcesses; }} Cout & lt; & Lt; "Newline closed"; Promotion :: this_thread :: sleep (boost :: posix_time :: seconds (1)); } Cout & lt; & Lt; "Not tested for input now"; }  

How can I do this?

See file descriptor service objects of ASIO.

A 'reactor' style in POSX is uninitialized, so you do not really need thread to get asynchronicity.

The loop reading my example shows that the 'exit' has been typed / or / when a deadline expires (10s).

  #include & lt; Boost / asio.hpp & gt; #include & lt; Boost / asio / posix / stream_descriptor.hpp & gt; Promotion :: ASO :: io_service my_io_service; Boost :: asio :: posix :: stream_descriptor (my_o_service, :: dup (STDIN_FILENO)); Promotion :: asio :: deadline_timer timer (my_io_service); // Timeout zero time-to-time indexing (boost :: system :: errorGode EC) {if (! Ec) std :: cerr & lt; & Lt; "Expired \ n"; Else if (ec == boost :: asio :: error :: operation_aborted) // Report this error timer.cancel () std :: cerr & lt; & Lt; "Initial departure :) \ n"; Else std :: cerr & lt; & Lt; "Getting Out for Any Other Reason:" & lt; & Lt; Ec.message () & lt; & Lt; "\ N"; Prevent reading loop in // cancell (); In.close (); } // set timeout timer zero arm_timeout () {timer.expires_from_now (boost :: posix_time :: seconds (10)); Timer.async_wait (timeout_expired); } // display loop without read_loop () {std :: cerr & lt; & Lt; "(Input is ...) \ n"; Steady boost: asio :: streambuff buffer; // some encapsulation :) async_read_until (in, buffer, '\ n', [& amp;] (boost :: system :: errorGode EC, size_t bytes_transfer) {if (! Ec) {std :: string line; std: : Istream (& amp; buffer); if (std :: getline (line, & amp; amp; amp; amp; line == & quot; exit & quot;) ec = boost :: asio :: error :: operation_aborted; else read_loop (); / / Release} if (EC) {std :: cerr & lt; "are going out for this reason:"  

On the windows, you can use the equivalent

You can add threads by running my_io_service.run () on more than one Are the thread


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -