[erlang-questions] Reference counting in NIFs

ANTHONY MOLINARO <>
Thu Aug 15 17:59:26 CEST 2013


On Aug 15, 2013, at 2:53 AM, Sverker Eriksson <> wrote:
>> 
>> 1. which enif_* functions change the reference count, and how?
>>  
> alloc_resource: ref=1
> release_resource: if (--ref == 0) free
> make_resource: ++ref
> keep_resource: ++ref
> 
> keep_resource and release_resource can also be called by the VM when resource terms are copied or destroyed.

Thanks for this information

>> 2. is there a better/safer way?
>>  
> Why don't use a public ETS table as a name lookup for your compiled regexps.
> 
> If you have to access shared data in NIFs from several threads then I suggest using the enif_mutex_* interface to do make it thread safe.

Well, because I had tried this and it failed to work, but I realized that I actually made a mistake in the way I tried and it does work.  So I'll benchmark this and see if it works.  

Thanks for the answers,

-Anthony


More information about the erlang-questions mailing list