Light weight threading
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:
2 Processors
3000 Threads
Light weight threads:
Done in 45 milliseconds
.NET threads:
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:
class Dog():
energy as int
def Eat():
energy++
def Bark():
energy--
[LightWeightThreadMethod]
def Live():
for i in range(100):
Eat()
Bark() 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 :)

Loading…
Brad Campbell
Very interesting. I am just starting to look into the possibility of trying to create some boo macros to make some parallel constructs for boo. I was thinking of things like parallel for loops similar to OpenMP in Fortran. You would simply write a for loop as normal, but instead of for, maybe you name it forp. Would you be willing to share some of your code so I can learn from it?
I’ve toyed with boo a little bit in the past but never done much with it. I’ve done quite a bit of C# and more fortran than I’d like to admit.
Thanks,
Brad
29 Mar 2007, 02:45
Add a comment
You are not allowed to comment on this entry as it has restricted commenting permissions.