Take the 2-minute tour ×

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


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?

share|improve this question
First of all, for generating an RSA keypair, you'd be better off using libssl than hacking together a system() call to invoke ssh-keygen... –  user529758 Jan 18 '13 at 19:13
add comment

2 Answers

up vote 5 down vote accepted

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.

share|improve this answer
^So, can I take your answer as 'One should avoid using system() in a multi-threaded program'? –  tormentorres Jan 18 '13 at 19:52
Perhaps not so strong, like "avoiding system() for multi-threaded program is usually/frequently well-justified". If I knew there is no fork/exec, and SIGINT/SIGQUIT/SIGCHLD handlers & masks are unimportant -- I could take the risk of using system. –  Anton Kovalenko Jan 18 '13 at 19:56
add comment

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.

share|improve this answer
add comment

Your Answer


