In programming, spawning a thread is creating a code execution path that will run in parallel with other tasks you’re doing. For instance, if I want to copy a big file, I can spawn a new thread, give it the location of the file and the destination of the copy and tell it to go off and do its thing. While it’s working, I can do other things like rendering on screen windows and such. When it completes, the file has been copied and the thread dies.

In some threads it’s useful to check in on it to see how it’s getting along. This is called polling. I’d ask the thread “How much have you copied?” and it would reply “33%”. I could take that number and give it to my progress bar that I’m showing and the file will keep copying as I do other stuff.

In other threads, I don’t want to have to keep asking it how it’s doing but I want it to tell me at major milestones how it’s progressing. This is called an interrupt or a signal. When I start the thread, I say “Every 5% of file copied, let me know.” Then I go off and do other stuff. While it works it says “I’m at 5%”, “I’m at 10%”… Each time it checks in, I pause what I’m working on and go update the progress bar with the new value.

The other day I went to the gym. The past few days after working out I had neglected to stretch afterwards and I paid the price with having post-workout-waddles. The sun was out and there was a nice breeze so I parked myself outside of the gym in the grass and stretched myself out.

All stretched out and relaxed I decided to try and get my vitamin D for the year. I didn’t want to get burned so I figured I’d lay there for about 15 minutes. I didn’t have a watch or my phone so I started counting my breaths. I was taking deep breaths at about 1 breath every 3 seconds so I figured I’d count to 300.

So, laying in the sun, just stretched out, warm, relaxed, taking deep breaths, counting…a perfect mixture for taking a nap. …8…9…10…11…

My mind started to wander and I started slipping into early stages of sleep. I’d daydream for a bit and then become more aware and realize I was still counting…45…46…47…

I slipped out again and then suddenly 100! It wasn’t a gradual realization that I was still counting like before, it was a sudden “There is important information that needs to be addressed.” I did a quick calculation to double check my original math (100 * 3 seconds / 60 seconds = 5 minutes) and continued counting…101…102…103…

So it wasn’t until I got home and started programming that I really connected what had just happened to threads. But it has a pretty striking resemblance. I spawned a thread that was a counter. I didn’t have to pay too much attention to it because breathing is automatic and all I had to do was increment that counter (compare this to trying to count seconds which requires initializing, updating, and trusting your internal clock, which I suck at).

With the thread running, I could poll the current count and didn’t have to actively keep track of it in the front of my mind. The really awesome bit was the interrupt. I didn’t plan “Okay, I’m going to check in at 100” but my guess is that at such a round even number, the part that was keeping track wondered what the hell all this counting was for and made sure it was still important.

This isn’t a perfect system though because at 300, I had gotten sufficiently relaxed that sleep logic started setting in and I messed up or just half assed the calculation and didn’t realize that it was time to get up. Or maybe I convinced myself that 20 minutes was better than 15. At any rate, a minute or so later, I polled the thread and got awake enough to actually do the calculation and decided it was time to get up.

It seems the first major interrupt at 100 was quite a bit stronger than subsequent interrupts. Even the one that was supposed to be the final. Maybe this is some sort of “well, you didn’t care last time, so you won’t care next time” subconscious learning going on. Or maybe I just fell more asleep.