From what I've seen in the game, there are plenty of places we could argue about that might be done more efficiently, however it is all conjecture until someone who has seen the code or taken the time to reverse enough of the assembly to understand what's going on decides to speak up.
The idea that you HAVE to have the source code to implement threading is wrong in my opinion. It would be amazingly obnoxious to do without the source code, but we do have the assembly and there are tools for changing it and making it more readable. Honestly its probably not worth the effort to do it in assembly though (Especially this close to the next release).
In the mean time, I have an idea I'm willing to be flamed on it:
And please note that anything I say about internals and structures of the game below is mostly just guesswork. It could be right, it could be close, it could be so wrong Toady bursts in to tears laughing at how inefficient the methods I "accuse" him of using are. It’s really all meant to be more of a discussion piece.
I had a lot more information in this post, but I’m hoping I can just post a summary here:
I assume DF has essentially one main game loop. This loop basically says take each individual dwarf, do dwarfy things, go to the next dwarf. When all the dwarves have been gone through, one “turn” is complete. (I know there are stones, gravity, temperature, other creatures, fluids and more to do on each "turn." I said the loop "basically" does this).
Now assume we have 100 dwarves and 4 processors (or a quad-core or 2x dual cores it doesn’t really matter). Instead of looping through 100 dwarves on 1 processor, we could loop through 25 dwarves on each processor. When all 4 of those loops are finish, one turn is complete.
(and it is important to have roughly equal numbers of each type on each processor. If we have 100 stones and 100 dwarves, putting 50 stones on 2 processors and 50 dwarves on 2 processors is not going to be as effective as 25 stones and 25 dwarves on each processor)
TO borrow G-Flex's example of baking a cake (since it is the only example I really saw in the thread on why multi-threading DF is hard):
- Preheat the oven to 350 degrees.
- Grease a pan.
- In a large bowl, beat the eggs.
- In another large bowl, mix the sugar, flour, and baking powder.
- Add the other liquid ingredients to the eggs and mix thoroughly.
- Add the egg mixture to the flour mixture and mix them together.
- Pour this into the pan.
- Stick the pan in the oven for 40 minutes.
Breaking up that list is hard. Having a computer do it is even harder. However what if we aren't making one cake. Let’s make 2. Now if you had to make two cakes, it gets a lot easier to split things up. One of the simplest is going to be to put two chefs in two different kitchens with the same list as above, and in roughly 50 minutes, you'll have two cakes.
It does get harder though. Let’s assume the chefs are in the same kitchen (with plenty of counter space and bowls and ovens). We've got plenty of everything except Grease. (No one brought any animals, we don't have a butchershop, whatever). With only one tub of grease for step 2, only one chef can grease his pan at a time. Now maybe we can make one chef grease his pan right before he pours the cake in to the pan, or maybe one chef is going to have a smoke break while the other greases his pan, the point is that they can't both grease the pan at once.
Some of the things that objects and dwarves do might be like the grease, but from the behavior I’ve seen in my dwarves, it appears to me that a lot of the protection functions would need to implement already exists. One issue I can think of is finding a new job for a dwarf to do. Previously only one dwarf could ever be looking for a job at once. There’s no need to protect that data if only one dwarf looks at once. But if we have 4 or 8 or maybe you play on a supercomputer with 2000 processors and have 2000 dwarves looking for a job at once, then you might start ending up with more than one dwarf picking the same job at the same time. You would probably end up getting a "Job Item misplaced" message at some point, but until it happened you'd have extra dwarfs just wandering in circles (trying to do a job till some other dwarf beat them to it).
If multi-threading is even possible in this way depends entirely on how the code and pathing algorithms are written. If they’re all accessing/changing shared memory all the time, then things could get extremely… FUN… (Though I’m not entirely sure programming FUN is as fun as dwarf FUN). But its doable with or without code. Its not that hard with some code.