All entries for Wednesday 24 May 2006
May 24, 2006
I'm experimenting with light weight threads in Boo. I have an AST attribute that duplicates a method and inserts "yield" statements between each statement. This makes the method into an iterator that can be stepped through. I have a scheduler class that takes a list of methods and effectively runs them concurrently – but without all the heavy weight OS context switching.
Here is sample output from a demo program:
Light weight threads:
Done in 45 milliseconds
Done in 50236 milliseconds
About 1000 times faster!!
Note that I have two processors so the light weight scheduler actually creates two OS threads on which to run the light weight ones. This makes maximum use of my machine!
The code being run by the thread is in this class:
So basically I'm saying create 3000 dogs and run them all at once! Normal threads crawl due to the huge amount context switching. Light weight threads work like a dream :)
energy as int
for i in range(100):