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/