c++ - Unblocking a thread that calls an external process -
i have next complete, compilable illustration (also available gist) spawns little number of threads (echo pass
) , 1 other (echo block; sleep 1; echo unblock
). sec names because when see block
in output, loop spins wheels though other 'pass' threads should have finished long before then. have expected output pass
many times, interspersed checking
(since main thread looping @ point) , unblock
@ end.
how can expected behavior?
i realize may symptom of using system
, i've been using stand multithreading solution since haven't received working alternative yet. note couple of things should change, i'm still researching solutions. may source of bug, though.
// -*- compile-command: "g++ -std=c++0x main.cpp;./a.out" -*- #include <iostream> #include <thread> #include <map> #include <unistd.h> typedef char status_t; /** * know i'm not supposed using system, don't know * else utilize in context. programme i'm starting is, * theoretically, arbitrary. * * @param command shell command run, such "pdflatex file.tex" * @param running sentinal boolean whether thread still * running * @param exit_code_buf buffer exit code * * @todo utilize std::{atomic,future,promise} appropriately... */ void run(std::string command, bool *running, status_t *exit_code_buf) { // todo: add together std::chrono::duration timeout *running = true; *exit_code_buf = system(command.c_str()); *running = false; } class processgroup { std::map<std::pair<std::thread *, bool *>, status_t *> threads; public: void add(std::string); void join(); }; /** * starts process in new thread , adds map * * @param command command start * * @todo utilize std::{atomic,future,promise} appropriately... */ void processgroup::add(std::string command) { status_t *status = new status_t(0); bool *running = new bool(false); std::thread *t = new std::thread(&run, command, running, status); threads.insert(std::make_pair(std::make_pair(t, running), status)); } /** * periodically checks threads see if still running. * if 1 of them is, sleep little amount of time , check * again. */ void processgroup::join() { bool still_running; { still_running = false; (auto = threads.cbegin(); != threads.cend(); ++it) { still_running |= *it->first.second; } std::cout << "checking" << std::endl; if (still_running) usleep (100000); } while (still_running); } int main() { std::cout << "program start." << std::endl; processgroup pg; std::string block("echo block; sleep 1; echo unblock"), pass("echo pass"); pg.add(block); std::cout << "here" << std::endl; (int = 0; i<10; i++) { pg.add(pass); } std::cout << "joining threads..." << std::endl; pg.join(); std::cout << "program end." << std::endl; homecoming 0; }
compiling…
$ g++ -std=c++0x main.cpp; ./a.out programme start. here joining threads... checking block checking checking checking checking checking checking checking checking checking unblock pass pass checking pass pass pass pass pass pass pass pass checking programme end. compilation finished @ fri oct 3 23:49:45
c++ multithreading c++11 blocking nonblocking
No comments:
Post a Comment