Malloc和nogil一起使用安全吗?

2024-04-27 08:51:03 发布

您现在位置:Python中文网/ 问答频道 /正文

用malloc和cython中的nogil分配内存安全吗? 另外,在多线程程序运行nogil时传递指针是否安全?在


Tags: 程序运行cython指针malloc分配内存nogil
1条回答
网友
1楼 · 发布于 2024-04-27 08:51:03

GIL之所以存在是因为CPythons内存管理不是thread-safe。因此,在不与Python对象(即Python处理的内存)交互的情况下,可以使用nogil。在

文档for releasing the GIL中提到了这一点:

Code in the body of the statement must not manipulate Python objects in any way, and must not call anything that manipulates Python objects without first re-acquiring the GIL. Cython currently does not check this.

因此,只要不涉及Python对象,使用malloc、传递指针和执行C中合法的任何操作都是非常安全的。在

相关问题 更多 >