Calling third pary multithreaded library in a NIF

Hi,
Is it safe to call third party multithreaded libraries in a NIF?

Erlang exposes dedicated functions for creating threads in a NIF like erlnif_create_thread so I am not sure whether calling some third party library in a NIF like ffmpeg which creates threads in a standard way is safe?

3 Likes

As long as the calls are non-blocking, it should be fine. The NIF thread creation functions are provided for portability.

If, on the other hand the 3rd party library calls are blocking, it would have adverse performance implications on the regular Erlang schedules, and the blocking calls would either need to be rearchitected to make sure that on the Erlang side the functions don’t block or mark those NIFs as dirty (see ERL_NIF_DIRTY_JOB_CPU_BOUND), so that those calls would be dispatched to the group of dirty schedulers, which is a separate group that doesn’t interfere with the work of the regular (i.e. IO-bound) schedulers. Upon completion of the function call, the process will be scheduled on one of the regular schedulers.

4 Likes

Thanks!

1 Like

erlnif_create_thread Creates a new thread.When the thread is blocked. Will it affect the beam vm?

1 Like

I assume what you are asking is if some function executed in the context of a thread created by enif_thread_create() blocks whether that would cause any kind of blocking in the beam vm schedulers? The answer is no, because the newly created thread has a separate execution context from the threads running regular Erlang schedulers.

1 Like

Thanks. I understand

1 Like