3 ms·
A mutex around fork() buys you nothing. The problem is other locks, which often are not even part of your code. The article has perhaps the most likely exampl
by asveikau 12y ago
A mutex around fork() buys you nothing. The problem is other locks, which often are not even part of your code.
The article has perhaps the most likely example. Imagine malloc() has a lock. Another thread happens to be inside malloc() at the moment that you fork(), and therefore owns the lock and might be in the middle of manipulating shared data structures. Now suddenly the child cannot malloc(), because that thread [suspended in the middle of its execution] isn't going to be carried over to it, and will never be able to clean up its intermediate state and release the lock.
- djcapelis 12y agoIt buys you plenty if you prevent other threads from entering until you all meet in a rendezvous lock.
- asveikau 12y agoThe worker thread that was kicked off by some random library dependency (acquiring a lock you didn't know about) isn't going to care about your rendezvous lock. Even if you do control all the threads getting them to do what you're suggesting may be nontrivial and costly. Edit: IMO it's better to just admit the programming model is thorny and move on. I feel similarly about signals. Restrict what you do after fork() and generally be cautious, the same way you'd be cautious reacting to a signal.
- djcapelis 12y agoWrap the thread create calls and make them care. :)