Ant simulation: it's better to create a Process/Thread for each Ant or something else? Ant simulation: it's better to create a Process/Thread for each Ant or something else? multithreading multithreading

Ant simulation: it's better to create a Process/Thread for each Ant or something else?


I would recommend to have a look at stackless. Stackless introduces tasklets which are akind of microthreads which allows to get the benefits of thread-based programming without the performance and complexity problems associated with conventional threads

A possible problem with stackless is, that as far as i know you need to use a modified interpreter or pypy to use the microthreads. However it might be worth it, as there are a some companies that use stackless with great success (eg. for it is used for EVE Online)

Also have a look at greenlet which also offers you a kind of microthreads, without replacing the interpreter. However compared to stackless greenlet only offers a limited featureset.


If you don't mind GPL, I suggest you use the Khronos simulation framework, which allows you to define each ant as a generator so you don't need to create threads. The Khronos engine takes care of the scheduling.

I'm actually developing a competing project called GarlicSim which you can also use for your simulation, but for your case Khronos would be better. (Unless you have a problem with GPL.)


I wrote an ant simulation (for finding a good TSP-solution) and a wouldnt recommend a Thread-Solution. I use a loop to calculate for each ant the next step, so my ants do not really behave concurrently (but synchronize after each step).

I don't see any reason to model those ants with Threads. Its no advantage in terms of run-time behavior nor is it an advantage in terms of elegancy (of the code)!

It might be, admittedly, slightly more realistic to use Threads since real ants are concurrent, but for simulations purposes this is IMHO neglectable.