Sorry for the stupid name: I could not think of anything better. I need to develop a program that will simulate a certain time line (not coinciding with the real, of course), for example, using the main loop, where each iteration will be the second of this imitation time line (as a "tick" in game engines); there will also be events at different moments of simulation time, there will be a lot of events, new programs may appear during program execution, and they should be processed in parallel. Let's say there is a queue where elements can be written to, and a handler where they are processed (unexpectedly). If the queue is full, then the element will return false , otherwise it will fall into the queue and will wait for the processing of all those ahead. The bottom line is that all this should happen in parallel, but I can not imagine a normal implementation. Now thoughts are only about a monstrous array, where events and timecode will be recorded, and in the main loop there will be a check, like this (pseudocode):

 for (int i = 1; i < renderTime; i++) { switch (eventLine[i]) { case "addItem": foo(); case "finalizeItem": bar(); } } 
  • one
    And why should everything really happen in parallel? Let it be parallel in the artificial timeline, but in reality you can do as you please. Choose a moment in time, select all events related to this moment in time, execute them, look for the next (nearest) moment in time, repeat until the queue is exhausted. - VladD
  • @VladD, that is, do you suggest to really implement an array of the form of действие => таймкод and follow it? - Urffly
  • one
    On the contrary, timecode -> action. And keep sorted by timecode. - VladD

1 answer 1

  • In my opinion here is a hint of TaskFactory. And asynchronous operation and control of the number of worker threads.

  • Or even use Task-out of the box, by controlling the system itself to determine the number of live threads in ThradPool-e. And actually your work on tick - for example, Task (() => doworkIteration1 ());

  • Just be sure to read about Task, async, await and what they eat with.