首页 > 解决方案 > 如何检查malloc是否持有锁

问题描述

我的代码面临如下问题:

1.A process was using malloc to allocate the memory.
2.While allocation process was interrupted and respective signal handler was 
called. 
3.in the signal handler again malloc was called, which leads to a hang.

现在经过一番谷歌搜索后,我知道我不应该在信号处理程序中分配内存。

有没有办法处理这个问题,不需要我避免在信号处理程序中使用 malloc?例如检查 malloc 是否已经在进行中或持有锁。

标签: c++memorylockingmallocsignals

解决方案


在信号处理程序内部,您只能调用async-signal-safe functions. malloc不是其中之一。不过,您可以尝试预先分配内存以用于信号处理程序(在安装信号处理程序之前)。


推荐阅读