[erlang-questions] NIF appropriateness, was: Re: Messing with heart. Port and NIF, which one is better?

Sverker Eriksson sverker.eriksson@REDACTED
Thu Feb 14 17:16:45 CET 2013


Max Lapshin wrote:
> Can enif_consume_timeslice help to mark nif-blocked pthread still active?
>   

Creating custom threads is one way to do lengthy native work while being 
nice to the VM.

Another way is to divide your work into smaller pieces and do repeated 
calls from Erlang to your NIF until the work is done. This is where 
enif_consume_timeslice (new in R16B) can be useful. You tell the runtime 
system an estimation <http://tyda.se/search/estimation?w_lang=en> of how 
much of the scheduling timeslice (about 1ms) you have consumed and 
enif_consume_timeslice will return back if your timeslice is exhausted 
or not.
Note that this is still co-operative scheduling. enif_consume_timeslice 
does NOT do any preemptive scheduling itself, you need to voluntarily 
return from your NIF in order to yield the scheduler thread to do other 
work.

For drivers, erl_drv_consume_timeslice will also be available in R16B.

/Sverker, Erlang/OTP, Ericsson



<http://tyda.se/search/voluntarily?w_lang=en>



More information about the erlang-questions mailing list