0

When designing my simulator, I have gotten stuck on 2 main design choices.

The simulator can be described as having X number of nodes (between 50 - 2000) that each need to independently do some processing during each time-step in their own thread. After they are all done processing, then the main thread needs to do some processing on the results. The main thread will then advance the time-step and this process will continuously loop until the simulation is over.

  1. How would the Nodes tell the main thread they are done without exiting if they are each in their own thread?

The nodes are stateful, so they must persist after each iteration. join() i believe only works if the thread is done executing, which would not work for me. Is there some mechanism where the nodes can notify the main thread and then the main thread can have them start another iteration?

3
  • 1
    The format of this community requires only asking one question per post. Can you edit your question to only focus on a single problem? Jul 15 at 13:28
  • Are you not familiar with the many common thread synchronization primitives like semaphores, events, etc which are used to coordinate stuff like this? Jul 16 at 13:20
  • I am familiar with semaphores in the context of using them to protect shared resources, not in the context of synchronizing threads. I actually didn't even think about using something like an event queue, that would be a good solution. Jul 18 at 13:45

1 Answer 1

1

You could give the node class a static countdown integer that you set to the number of nodes before releasing the node threads for a next iteration. Each node could decrement countdown when done (InterlockedDecrement or within a lock). Once zero the main thread would know all nodes are done.

But, it is not so great to let each node have its own thread. This does not scale well and it is needlessly complicated.

You would be better off having the main thread (or a single worker thread) call every node's Go() method in a loop. This also elegantly makes your problem disappear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.