Rip Van Winkel – the sleep story

A while back I started coding a front-end for cdrecord/growisofs called Toasteplex that would allow burning multiple copies of a disc simultaneously. My dad was using it for the ministry, but had stopped for a while. Recently he’s been wanting to use it again, so I started getting the code cleaned up and fixing some bugs.

At one point in the code I was using wxWidgets thread handling code when I really should have been using native pthreads. It wasn’t a big deal to get it ported over; pretty much every function call had a one for one equivalent. During that conversion, I ran across a call to wxThread::Sleep. Pthreads doesn’t have it’s own sleep function, so I happliy replaced that call with a call to the posix standard sleep function. The original waiting time was 800 milliseconds, so wxThread::Sleep(800) became sleep(800).

Wrong!

The posix sleep function takes seconds, not milliseconds! It took me around a week to find that bug. I thought I was forgetting to unlock a mutex or a condition was never being met. Debugging a multithreaded program is terribly difficult..at least it is for me.

Just remember kids, take sleep seriously. If you’re not careful, you can go from sleeping for 800 milliseconds to sleeping for 13 minutes. That’s a long time for one iteration of a while loop. šŸ˜‰

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: