Why is malloc not async signal safe?
Why can't malloc be used in signal handlers? What ca开发者_如何学编程n "happen wrong"?
A signal handler can be called at any time, including during times when another call to malloc is in progress. If this happens, one of two things will occur:
- Your process will deadlock inside the signal handler, because
mallocwill be unable to acquire the heap lock. - Your process will corrupt its heap, because
mallocdoes acquire the lock (or doesn't think it needs it), then proceeds to render the heap inconsistent, leading to a later crash.
加载中,请稍侯......
精彩评论