开发者

Erlang and C/C++ Threading

开发者 https://www.devze.com 2023-03-01 11:36 出处:网络
If I use erlang as say a spawner process, it开发者_如何学JAVA does major functions things that are not too speed critical like communicating with a server and handling server-client communication mess

If I use erlang as say a spawner process, it开发者_如何学JAVA does major functions things that are not too speed critical like communicating with a server and handling server-client communication messages.

Then I choose to spawn a process in erlang and run c/c++ code from it, will this make my code faster?

Will erlang more efficiently handle multithreading than an equivalent in c/c++?

If I were to spawn many c nodes from erlang, would they stack on a single core or would erlang handle the multithreading. This is the main point I am wondering about.


You talk about two different concepts in your question: Erlang processes running C/C++ code and C nodes.

C or C++ code run from inside Erlang is not scheduled at all. In fact, it will block the Erlang scheduler for the current CPU core, so having long run times in C-land will most likely mess up your (Erlang) scheduling.

For example, on a quad core processor Erlang will by default create 4 scheduler threads, each which will occupy one core. Any process running C code will block the scheduler it is assigned to until that code has finished executing.

When running C nodes, you're totally on your own. An Erlang node does not know about the scheduling of a C node at all, it only cares about it's own scheduling. Of course, you could create your own scheduling such as dedicating one core to the C node and three to the Erlang node or something similar.


Erlang only manages concurrency using its own lightweight processes, which are not thread based. It usually only runs a few threads that service the many thousands of processes it can spawn, which are usually only one thread per CPU/core when SMP is enabled.

It definitely doesn't know anything about any external threads, and any external processes will just add communication overhead and complexity of managing memory and resources in the external process.

0

精彩评论

暂无评论...
验证码 换一张
取 消