Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is cpp-subprocess thread safe? #20

Closed
lordadamson opened this issue Dec 24, 2018 · 5 comments
Closed

is cpp-subprocess thread safe? #20

lordadamson opened this issue Dec 24, 2018 · 5 comments

Comments

@lordadamson
Copy link

lordadamson commented Dec 24, 2018

Or does it provide the option of thread safety?

In my code I'm using an openmp loop where I would like to call sp::Popen.

It looks something like this:

#pragma omp parallel for
for(size_t i = 0; i < v.size(); i++)
{
	auto p = Popen({"subprocess"}, input{PIPE});
	p.communicate(v[i].c_str(), v[i].size() + 1);
}

where v is a std::vector<std::string> but it hangs randomly at the fork() call in subprocess.hpp:1159

Thanks in advance and thanks for the amazing library.

@arun11299
Copy link
Owner

Hey! Thanks for reporting the issue. Also, Merry Christmas!!
I will take a look at it and get back to you.

@lordadamson
Copy link
Author

Merry Christmas to you too 😄

@arun11299
Copy link
Owner

I tried reproducing this with std::thread instead of Open MP:

void test_multithreaded_exec()
  {
    auto thread_fn = []() {
      auto p = sp::Popen({"cat", "-"}, sp::input{sp::PIPE}, sp::output{sp::PIPE});
      auto msg = "through stdin to stdout";
      auto res_buf = p.communicate(msg, strlen(msg)).first;
      assert(res_buf.length == strlen(msg));
    };

    std::vector<std::thread> threads;
    for (int i = 0; i < 100; i++) {
      threads.emplace_back(std::thread{thread_fn});
    }
    for (int i=0; i < 100; i++) {
      threads[i].join();
    }
    std::cout << "END_TEST" << std::endl;
  }

Popen object if used within a thread context should be thread safe. Is there any Open MP specific thing going on here ?

@lordadamson
Copy link
Author

Hi @arun11299 I failed to reproduce the issue on an MCVE.
That suggests that there is a memory corruption happening somewhere else in my code that's causing the issue.

Thanks a lot for looking into this. I'll investigate further and will let you know if I have any useful findings.

@arun11299
Copy link
Owner

Sure. Let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants