boost::process::pstream not thread safe?
- Dominant language
- C++
- Stars
- 145
- Forks
- 151
- PR merge metrics
- No merged PRs in 30d
Description
I thought that `boost::process` could allow me to convert what is becoming a rather involved shell script into a C++ program. It is of the usual "run the same program lots of times with different inputs then process the results" variety, with the magical "&" character in the shell script.
For a quick test `/bin/cat` is the program:
```
// boost::process::pstream not thread safe?
// Related to
// https://github.com/boostorg/process/issues/206
// https://github.com/boostorg/process/issues/169
// https://github.com/boostorg/process/issues/170
// ?
#include
#include
#include
#include
#include
#include
namespace bp = boost::process;
int job(const int n)
{
#if 1
bp::ipstream is;
bp::opstream os;
bp::child c("/bin/cat", bp::std_in < os, bp::std_out > is);
os << "job " << n << '\n';
os.close();
// https://github.com/boostorg/process/issues/125
os.pipe().close();
std::string line;
while (c.running() && std::getline(is, line)) {
// do something with line locally
}
c.wait();
if (c.exit_code()) { std::cerr << "Ooops!\n"; exit(1); }
is.close();
#endif
return n;
}
int main(int argc, char **argv)
{
int nproc;
if (!(argc == 2 && (nproc=std::atoi(argv[1])) > 0)) {
std::cerr << "Usage: " << argv[0] << " n_processes\n";
exit(1);
}
std::list> jobs;
for (int n = 1; n <= nproc; ++n) {
jobs.push_back(std::async(std::launch::async, job, n));
}
while (!jobs.empty()) {
std::cout << "got " << jobs.front().get() << '\n';
jobs.pop_front();
}
}
```
- When run with `n_processes=2`, `cat` hangs in state `pipe_r`.
- When compiled with `#if 0`, the `async` side of the program seems fine.
- Everything in job() apart from `n` is local from the C++ point of view, but presumably some file descriptor sharing is going on underneath...
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.