Home > database >  Spawning 10,000 threads doesn't seem like the right approach, alternative ideas?
Spawning 10,000 threads doesn't seem like the right approach, alternative ideas?

Time:03-19

I am trying to simulate a decentralized system, but having trouble simulating given the real-life parameters.

Real-world:

  • Each module has its own computer and they communicate over a network
  • There can be hundreds of thousands of modules
  • They will communicate with each other to perform a certain action

Simulation:

  • Each module is considered its own thread since they are working async
  • Can't really spawn more than 1,000 threads
  • The thread to the module ratio is 1 to 1

Is spawning a thread per module the right approach? In theory, this seems like the right approach but in practice, it hits limitations at around 1,000 threads.

CodePudding user response:

Your context perferfectly match with the actor model
https://en.wikipedia.org/wiki/Actor_model
Explaining it through a response is impossible, start from the wiki link and search for some implementation in the language your are using, but it does what you need, you can simulate milions of "isolated states" and manage the concurrency of their mutations using very few resources (you should be able to reach 1K actors with very few threads, maybe also 2).

Also, nowadays a lot of languages offers (in their flavour) a version of lightweight threads that can be used to reduce the number of real threads used (goroutine, kotlin coroutines, java fibers, etc..)

  • Related