Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make sure N threads run at roughly the same speed?

I'm toying with the idea of writing a physics simulation software in which each physical element would be simulated in its own thread.

There would be several advantages to this approach. It would be conceptually very close to how the real world works. It would be much easier to scale the system to multiple machines.

However, for this to work I need to make sure that all threads run at the same speed, with a rather liberal interpretation of 'same'. Say within 1% of each others.

That's why I don't necessarily need a Thread.join() like solution. I don't want some uber-controlling school mistress that ensures all threads regularly synchronize with each others. I just need to be able to ask the runtime (whichever it is---could be Java, Erlang, or whatever is most appropriate for this problem) to run the threads at a more or less equal speed.

Any suggestions would be extremely appreciated.

UPDATE 2009-03-16

I wanted to thank everyone who answered this question, in particular all those whose answer was essentially "DON'T DO THIS". I understand my problem much better now thanks to everybody's comments and I am less sure I should continue as I originally planned. Nevertheless I felt that Peter's answer was the best answer to the question itself, which is why I accepted it.

like image 647
lindelof Avatar asked Mar 13 '09 09:03

lindelof


People also ask

How do I make sure threads run in order?

Sequencing in multi-threading can be achieved by different means but you can simply use join() method of thread class to start a thread when another one is finished its execution. To ensure three threads execute you need to start the last one first e.g. T3 and then call join methods in reverse order e.g. T3 calls T2.

How does thread ensure faster processing?

Threads use the memory of the process they belong to. Inter-process communication is slow as processes have different memory addresses. Inter-thread communication can be faster than inter-process communication because threads of the same process share memory with the process they belong to.

Do threads actually run at the same time?

Concurrency and Parallelism Concurrency indicates that more than one thread is making progress, but the threads are not actually running simultaneously. The switching between threads happens quickly enough that the threads might appear to run simultaneously.

Can 2 threads have the same priority?

Threads have some priority. Because of that thread scheduler assign processor to thread. It is possible to have same priority to threads.


2 Answers

You can't really do this without coordination. What if one element ended up needing cheaper calculations than another (in a potentially non-obvious way)?

You don't necessarily need an uber-controller - you could just keep some sort of step counter per thread, and have a global counter indicating the "slowest" thread. (When each thread has done some work, it would have to check whether it had fallen behind the others, and update the counter if so.) If a thread notices it's a long way ahead of the slowest thread, it could just wait briefly (potentially on a monitor).

Just do this every so often to avoid having too much overhead due to shared data contention and I think it could work reasonably well.

like image 79
Jon Skeet Avatar answered Oct 14 '22 01:10

Jon Skeet


You'll need some kind of synchronization. CyclicBarrier class has what you need:

A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released.

After each 'tick', you can let all your threads to wait for others, which were slower. When remaining threads reach the barrier, they all will continue.

like image 41
Peter Štibraný Avatar answered Oct 14 '22 01:10

Peter Štibraný