Believe it or not, but I spent most of last week trying to fix a rather annoying issue with the application I’m writing for my internship.
The application’s multi-threaded, since it has to do multiple things at once: Listen for incoming data and add that onto the stack, which is processed in a second thread. To keep the UI from hanging, that’s got a dedicated thread as well. When I’m doing testing, there’s also a fourth thread that keeps itself busy with sending dummy data to the application. All fine and dandy, but I have to keeps tabs on what thread is using what data, to make sure they don’t fight over it and mess things up.
This whole juggling of threads and their use of pieces of data can get a bit tricky. You have to think long and hard about any and all possible scenarios that might arise and make sure all of those are safe and trouble-free, otherwise you’ll find yourself searching for a stupid issue that only happens ten percent of the time. Not even kidding, this kind of stuff can be the worst to debug.
It’d pop up with a warning, telling me something was up. After continuing program execution despite it, everything would go to shit. Yeah, expected that, thanks to the warning. So I focussed all my attention on the warning and tried to work from there. My field of vision narrowed and I didn’t see the rather obvious flaw. If the first two messages arrived in quick succession, it’d fire up two threads for processing said messages. This because it wouldn’t get the time to recognize, “hey, we’re starting the whole processing thread, so no need to make a new one” in between messages, making it think “looks like we ain’t busy yet, let’s kick things into gear”.
Adding, what, five lines of code total fixed the issue. And it took me almost three days. Fuck.