gpt4 book ai didi

python - cdef extern from ... 诺吉尔

转载 作者:太空宇宙 更新时间:2023-11-04 00:20:51 24 4
gpt4 key购买 nike

关于像这样应用于 cdef extern 的“nogil”,有一件事我不完全理解:

cdef extern from "pthread.h" nogil:

ctypedef struct pthread_mutex_t:
pass

cdef int pthread_mutex_init(pthread_mutex_t *, void *)
cdef int pthread_mutex_destroy(pthread_mutex_t *)

我不清楚那个 nogil 的效果,我在 Cython 的文档中找不到任何关于它的信息。我的解释是,在内部声明的函数隐含了 nogil。我对吗?那么结构呢?

感谢您提供任何信息。

最佳答案

来自 Cython docs关于与 C 代码的绑定(bind)

The nogil function annotation declares that it is safe to call the function without the GIL.

在您的示例中,这意味着 with nogil block 中允许使用函数 pthread_mutex_initpthread_mutex_destroy。在没有显式 with nogil block 的情况下,GIL 仍然存在:有必要但不足以像示例中那样声明函数。

普通 C 变量可以在 nogil block 中使用,但您要对线程安全负责。

关于python - cdef extern from ... 诺吉尔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49162218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com