erlangerlang-ports

Erlang ports and thread safety


I'm looking to call a C function from an Erlang process via an Erlang port, as described here:

http://www.erlang.org/doc/tutorial/c_port.html

In production I will need multiple Erlang processes calling the C function in parallel, each with a different set of arguments.

My question is, will this be thread safe at the C function level ?

The docs talk about the controlling Erlang process creating a 'connected process', which it sounds as if is responsible for creating an isolated instance of the 'external program' (C function).

So it sounds like it's thread safe at the C level but I'd like to be 100% sure.

TIA


Solution

  • It may depend on your implementation, but with Ports, the answer is almost definitely "yes" for the reason you mention. If you were using a NIF and using shared memory inside of the NIF, you would have some concern for thread safety.

    With ports, however, the "controlling process" acts as a serialization (as in arranged in a series) layer, meaning requests are handled one after the other and not all at once. Moreover, I believe (but do not know for certain) that the communication protocol ports use also requires this serial execution.