[erlang-questions] enif_send, how to call off a smp thread?

Sverker Eriksson <>
Tue Mar 21 11:51:37 CET 2017


enif_send docs says:

"The message environment msg_env with all its terms (including msg)
is invalidated by a successful call to enif_send. The environment is to
either be freed with enif_free_env of cleared for reuse with 
enif_clear_env."

which means you have to call enif_clear_env before reusing msg_env
in the second call to enif_send.

Or if you're running OTP 19 you can pass msg_env as NULL,
in which case the message term is copied and its environment left intact
by enif_send.


/Sverker, Erlang/OTP


On 03/21/2017 05:10 AM, Vans S wrote:
> I am having a weird issue with enif_send.
>
> What is happening is I am interoping with a 3rd party C library which has
> its own event loop and procs callbacks from its own thread.
>
> When the callback procs (in the C NIF) I want to call enif_send to get
> the message back to an erlang pid.
>
> When I call enif_send the first term I pass it works, then every subsequent message
> gets dropped unless its exactly the same as the first time passed.
>
> For example
>
>
> static ErlNifPid my_temp_pid;
> static void on_callback(void* user_data)
> {
> ErlNifEnv* msg_env = enif_alloc_env();
> enif_send(NULL, &my_temp_pid, msg_env, mk_atom(msg_env, "okk"));
> enif_send(NULL, &my_temp_pid, msg_env, mk_atom(msg_env, "aaokk"));
> enif_free_env(msg_env);}
>
> *on_callback proced
> 6> receive X-> X after 1 -> none end.
> okk
>
> 7> receive X-> X after 1 -> none end.none
> *on_callback proced
> 8> receive X-> X after 1 -> none end.
> okk
> 9> receive X-> X after 1 -> none end.
> none
> *on_callback proced
> *on_callback proced
> 10> receive X-> X after 1 -> none end.
> okk
> 11> receive X-> X after 1 -> none end.
> okk
> 12> receive X-> X after 1 -> none end.
> none
> What is the correct way to do this? Thanks.
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list