2024-04-27 08:51:03 发布
网友
用malloc和cython中的nogil分配内存安全吗? 另外,在多线程程序运行nogil时传递指针是否安全?在
GIL之所以存在是因为CPythons内存管理不是thread-safe。因此,在不与Python对象(即Python处理的内存)交互的情况下,可以使用nogil。在
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中合法的任何操作都是非常安全的。在
malloc
C
GIL之所以存在是因为CPythons内存管理不是thread-safe。因此,在不与Python对象(即Python处理的内存)交互的情况下,可以使用
nogil
。在文档for releasing the GIL中提到了这一点:
因此,只要不涉及Python对象,使用
malloc
、传递指针和执行C
中合法的任何操作都是非常安全的。在相关问题 更多 >
编程相关推荐