tormentorres
tormentorres

Reputation: 13

Using system() in a thread C++

I want to use the method system() in a (non-main) thread(pthread) in C++. For example,

system("/path/to/some/script.sh");

Is this permitted? If so, is it safe and are there any precautions I should take?

The reason I'm asking is that I've had the following comment from a code reviewer:

"The rule is system() can only be called from a single-threaded process. I think you need to move your new code to a separate application."

Is the first sentence of the comment valid?

Upvotes: 1

Views: 2829

Answers (2)

Omnifarious
Omnifarious

Reputation: 56048

I wouldn't do it for a wide variety of different reasons, the problem with signal masks just being one.

In general, fork and threads are a tricky mix and need to be handled with care. The existing library functions were likely not written with a multi-threaded program in mind.

Upvotes: 0

Anton Kovalenko
Anton Kovalenko

Reputation: 21507

As of GNU/Linux implementation of system, it modifies the process signal mask during command execution. In multithreaded program, we're in for nasty surprises, e.g. if another thread forks at the same time.

Upvotes: 6

Related Questions