Now there are 1000 threads in c # that are started, executed, closed in 1 minute. That is, about 16 threads are received per second.

Computer:

  1. i7
  2. 4 real cores
  3. 4 virtual cores

When the program is running, the load of all cores is up to 30-49%

Question. What do we get with streams on elrang? A similar situation 16 threads per second or erlang will be able to increase this number of threads per second? Or do we have here physical limitations on the cores, that is, on average, somewhere 2 (4) C # thread per 1 (2) processor cores?

  • As far as I know, the erlang is notable for the fact that it does not rest on the protection of the system and the threads can be produced until the memory runs out. Since all this is spinning on the erlangʻa virtual machine and there is no binding to hardware or OS. - BiMaWa
  • one
    What is this application for you in which there are 1000 threads ?? I have only 485 threads running at the moment - and this is in the whole operating system! - sp7
  • @ prog432, but what does each thread do and what problem do they all solve together? Without an understanding of this "kitchen" nothing definite can be said. - avp
  • Well, in general, erlang is not designed for 1000 threads, but for a million and more. It also allows you to collect cluster systems and take over the entire interaction routine. For Proger, this is one multisystem with threads. Highly competitive systems are written on it. Therefore, this is definitely not for the desktop, although some are perverted . Well, this is me for reference, and as if there is no c # near ... too narrow Erlang`a specialization. With high loads, even Java loses. - BiMaWa
  • @ prog432, Please complete questions according to the rules of the community, otherwise they will be deleted. - Nicolas Chabanovsky

2 answers 2

Erlang streams cannot be directly compared to Java operating system streams.

Erlang implements the concept of lightweight threads ( green threads ). In this case, in reality, the emulator can use a much smaller number of threads of the operating system.

The conceptual difference is that in erlang the flow, or as it is called in local terminology, the process is created for a small task that it solves. Thus, each process processes incoming messages using an internal state. Erlang thus implements the actor model .

It is necessary to compare erlang processes with actors on C #.

Operating system streams are usually not created in such a large amount. In the case of using threads to speed up calculations, their number is usually chosen in such a way as to provide the necessary level of load. In the simplest case, if the threads are not idle while waiting and it is necessary to fully utilize the system resources, the number of threads is chosen equal to the number of processor cores. To perform a large number of tasks in this case, use a pool of pots (thread pool). Tasks are distributed across this thread pool, and threads are not restarted.

Light streams can be created in much larger quantities, because in their case the runtime completely controls the flow and can interrupt it at predetermined locations, thus the costs of synchronization, saving and restoring the flow state are greatly reduced compared to the operating system threads.

    To begin with, since your machine has 8 cores, you cannot physically parallelize the computation into more than 8 threads, regardless of the language. If you create more than 8 threads, some of them will be idle (and the operating system's scheduler will make sure that the threads are more or less evenly).

    Then, running 1000 threads at the same time is a very non-optimal solution for modern C #. Starting a stream is an expensive operation. If you need parallel execution of thousands of calculations , it is better to represent them in the form of Task 's, and run in parallel: TPL will take care to execute them without creating unnecessary additional threads. To do this, you have to do something like

     await Task.WhenAll(dataArray.Select(d => Task.Run(Compute(d))); 

    or use Parallel.For .

    Erlang streams do (approximately) the same as Task 'and C #.


    And if your tasks are involved in I / O or communication with asynchronous sources of information, then the benefits of switching to Task 'and async / await are even more: idle tasks will not take up any flow at all !