Zach Burlingame
Programming, Computers, and Other Notes on Technology

Simple Reader-Writer Lock in C on Win32

In a particular native C Win32 application, I have a few threads that regularly read a particular set of information while performing their main work routines and a single thread that updates that information. A readers-writer lock is well suited to a workload that is heavily read based with scarce writes.

The Win32 API provides a Slim Reader-Writer Lock. The problem is that it wasn’t added until Vista and I still need to support Windows XP. I wasn’t too keen on writing my own as writing thread-safe code – particularly synchronization objects – is notoriously tricky. A quick search turned up several solutions for a reader-writer lock in C++, but not too many in C. I was even less keen on using a fully-featured RWLock that wasn’t from a mature and active project or porting an implementation from C++. Fortunately, a basic RWL is not that difficult as far as synchronization objects go.

I decided to roll my own and I’ve placed the project on Bitbucket here. As I come across other threading needs, I’ll add any functions and utilities to it. There are certainly no guarantees that my implementation is bug-free but I did at least give a bit of thought. If you find anything, please share so that I can fix it!

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*