Threads
Threads! My experience with them is pretty limited. I know conceptually how they work, but I just have run into many situations where they would offer much advantage. Until now.
Now that we are redoing our tic tac toe game in Clojure, I have another chance to tackle minimax. I managed to get minimax working in a reasonable short amount of time, so I figured, maybe I should work on some optimization. If the computer went first, it was taking a little longer than I wanted for the first move to be picked. After some digging, I found an interesting solution using threads. If I mapped over my initial board using separate threads for each recursion, I could now have up to 9 minimax’s running concurrently! That is a pretty drastic reduction in the time it takes to run things.
Get Bjorn Johnson’s stories in your inbox
Join Medium for free to get updates from this writer.
The only problem with this is that when the game was over, the program just sat there with a blinking cursor, never exiting. Bummer. Setting up the map was just a matter if changing map to pmap. So why us my program hanging.
Luckily the answer was equally trivial. It turns out that clojure was not cleaning up all of these new threads I created, that is up to me. All you have to do to remedy this situation is throw (shutdown-agents) at the end of main and your threads will be killed!
Pretty cool and much quicker! Much better than my naive solution to randomly pick a known good space as a starting move.





