c - Erlang NIF : Threads locking

标签 c erlang erlang-nif

NIF 实现可以使用常规 C/C++ 线程锁定原语还是必须使用 NIF API(enif_mutex_lock(..)、enif_mutex_create(..)、 等)

最佳答案

来自 nif docs

Threads and concurrency

A NIF is thread-safe without any explicit synchronization as long as it acts as a pure function and only reads the supplied arguments. As soon as you write towards a shared state either through static variables or enif_priv_data you need to supply your own explicit synchronization. This includes terms in process independent environments that are shared between threads. Resource objects will also require synchronization if you treat them as mutable.

所以没有什么可以禁止你做任何你想做的事。您可以轻松地编写自己的互斥体/信号量等等。您可以使用 C 或 C++ 或 Rust 来完成。

就是说,没有什么能阻止制动一切。如果你破坏了任何东西,你就会在整个 VM 中破坏它。我会尝试使用标准的 Erlang 方式做事,尤其是在处理线程时。这些都是经过验证的方法,我还没有找到任何理由用其他方法替换它们。

关于c - Erlang NIF : Threads locking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26773125/

相关文章:

erlang - 为什么 nif 函数会阻止 Erlang VM 调度其他进程?

c - NIF 调用 Erlang 后二进制值发生变化

C - 请求不是结构或 union 的成员

C 表达式必须有整型或枚举类型?

erlang - 16#00 是什么意思?

Erlang Rebar escriptize 和 nifs

c - 如何在c中将秒添加到当前时间?

c - 如何调用函数并使用其结果?

erlang - 为什么大公司使用 Mnesia 而不是使用 Riak 或 CouchDB

ssl - 如何在 Erlang/YAWS 中启用 ssl/crypto