std::map<char, bp::child> children;for (char name = 'a'; name < 'k'; ++name) { std::ostringstream command; command << "echo 'hello from " << name << "';"; command << "sleep " << (rand()%900 + 101)/1000.0 << ";"; command << "echo 'bye from " << name << "';"; command << "exit " << (rand()%42) << ";"; auto& p = children[name]; p = bp::child( sv{"/bin/sh", "-c", command.str()}, bp::on_exit(std::bind(exit_handler, std::ref(p), _1, _2)), io);}work.reset(); // allow io to be finishedwhile (io.run_one()) { // wait for that std::cout << "Still running: "; for (auto& [name, child] : children) { if (child.running()) std::cout << " " << name; } std::cout << std::endl;}std::cout << "Bye\n";打印hello from ahello from bhello from chello from dhello from ehello from fhello from ghello from hhello from iStill running: a b c d e f g h i jStill running: a b c d e f g h i jhello from jbye from iStill running: a b c d e f g h jProcess 30748 exited with -1Still running: a b c d e f g h jbye from eStill running: a b c d f g h jStill running: a b c d f g h jProcess 30739 exited with -1Still running: a b c d f g h jbye from cStill running: a b d f g h jStill running: a b d f g h jProcess 30735 exited with -1Still running: a b d f g h jbye from bStill running: a d f g h jStill running: a d f g h jProcess 30733 exited with -1Still running: a d f g h jbye from hStill running: a d f g jStill running: a d f g jProcess 30744 exited with -1Still running: a d f g jbye from dStill running: a f g jStill running: a f g jProcess 30737 exited with -1Still running: a f g jbye from gStill running: a f jStill running: a f jProcess 30743 exited with -1Still running: a f jbye from fStill running: a jStill running: a jProcess 30740 exited with -1Still running: a jbye from jStill running: aStill running: aProcess 30749 exited with -1Still running: abye from aStill running:Still running:Process 30732 exited with -1Still running:Bye 这篇关于Boost ::按ID处理子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-13 21:33