Monday, February 2, 2009

Portable condition variables in Mysql codebase

How can you write good multi-threaded code without using condition variables?

On most flavors of unix you have the pthread api which gives you pthread_cond_t structures and the related API functions. However on all version of Windows before Windows Vista and Windows Server 2008, the Windows API's did not come with any kind of condition variables. This makes it really hard to write multi-threaded code that works on both Windows and Unix systems especially if you want the code to behave in a similar way on all platforms.

Well Mysql developers solved this issue by writing their own version pthread_cond_t and related functions which can be found in the codebase in the files my_wincond.c. The code is #ifdef enabled on Windows platforms otherwise the standard pthread api is used. The implementation of condition variables in the mysql code base utilizes one CRITICAL_SECTION variable, and 3 arrays of Windows' Event objects. The code is short but hard to truly understand. Please do take a look at this code.

There have been several other attempts by people to create Windows versions of Condition variables, which you can find by searching the web. Almost always these solutions include notes about how the solutions are not perfect.

Thankfully Microsoft has released Condition variables as part of their latest OS versions which they are supporting as part of the OS and system APIs. I don't know if Mysql developers are going to add #ifdef's to use these new APIs when available but I sure think it would be a good idea.

Ivan

3 comments:

  1. Hi Stewart,

    Do you know if someone else is working on a patch to use the latest Windows condition variables? If not I can take a look at it and see if it is straight forward to patch.

    Thanks,
    Ivan

    ReplyDelete
  2. Buying a piece of jewelry for him is thpmas sabo not as tricky as it might seem.thoughtfully-picked piece of jewelry. thomas sabo charms Here are five suggestions for when you are considering buying thomas sabo bracelets jewelry for him:A timepiece: Every man needs a reliable timepiece. thomas sabo charm carriers You have three different options to work with: thomas sabo watches A dress watch: Men with office thomas sabo charm pearl jobs need a watch that complements their suits.

    ReplyDelete