Tiny threads again 🧵
I want a tune to play while my Thumby game is doing something else.
Previously, I tried a new thread for every tune: didn't quite work
Plan B is a function list to which I append a tune function when I'd like it to play. Another thread notices that the list grew, and plays the tune. It seems that appending and reading lists on different threads is safe. I hacked up a proof of concept for this, and it works. Naturally, further issues arise.
Issue: debugging is hard
What are those threads doing? I need a log. I can't write to the screen because it's ridiculously small and it's needed for the game. So the log is a file. A mutex means only one thread uses the file at once. Seems simple.
Issue: logging to a file hangs the Thumby
We start off OK, but after a while both threads stop logging and the display stops updating. I'm fairly confident that the threads can't deadlock, but pick any combination of:
- I've messed it up
- Micropython file writing is doing something odd
- Micropython threading is doing something odd
If I comment out the file logging, all is well.
I note that the documentation for the _thread module says:
This module is highly experimental and its API is not yet fully settled and not yet described in this documentation.
As much as I might laugh in the face of danger, seems like I've dug myself a hole.
Way out of the hole?
The second thread does play tunes and doesn't need to write to a file. So I have what I wanted, as long as nothing unexpected happens and I need a log. What could possibly go wrong?
Next step: apply this stuff to the actual game, not the quick hack...