Pthreads and the Principle of Least Surprise

by Matt 31. July 2006 17:03

I've inherited some Unix code (see, it's not *all* Microsoft stuff). It's prototype code, but very full featured. One thing it didn't come with was a build system of any kind, no makefile, nothing. So, I've had fun and games (as ever) and put it into Eclipse (running locally on a VPC image, accessing the files via Samba and compiling over rsh. Oh, and I'm doing Test-Driven Development in C++. Is this actually the definition of masochism?)

I've just spent the last 2 days tracking down a very tricky/silly bug. This program uses libxml, which supports threads. This means Pthreads.

Pthreads is just an API standard. POSIX threads defines a set of functions, and the behaviour of those functions. It doesn't sepcify their implementation, and so you can get loads of different versions - kernel mode, user mode, pre-emptive, co-operative, operating system provided, compiler provided, 3rd party library provided, and it's very hard to know what to do.

So I did the obvious thing, and leant on the tools to help me out. Eclipse was handling creating the makefiles, and I very quickly got a clean compile. Excellent. Came to do some testing around the libxml usage, and it all went terribly wrong. Sometimes the tests would work, other times not. I eventually got it failing consistently, and in the debugger, and it became apparent what the problem was. The pthread code in libxml was failing. An initialisation routine that was only supposed to be called once (via a call to pthread_once) wasn't getting called, stuff wasn't getting initialised, bad things were happening.

And the Principle of Least Surprise? Turns out libc has a default implementation of Pthreads that does nothing. A whole bunch of nops. In the default library. If you don't explicitly link against the Pthreads library, your code will not work. You cannot rely on the linker telling you that you haven't linked correctly - you'll just link with broken code. Hardly the Pit of Success.

I can understand the rationale behind it, but it's flawed. The idea is that if you have a single-threaded program that uses a multi-threaded library, you can still compile and use the program. After all, it's a single threaded program, so your mutexes and condition variables and so on will all just work. But what if your library wants to create a thread for background processing such as garbage collection, asynchronous web service calls? Won't work. Single call initialisation functions? Won't work.

And that's by default.

It would have been far better to have had an explicit, no-op Pthread library you can link with if you really have to, rather than default you to a library that just plain doesn't work.

Just further proof that compiling code != working code.


The last word on mouse cursors

by Matt 25. July 2006 02:59

Honestly; I'm beginning to bore myself.

I just wanted to point out that using the fancy new Aero cursors, or the rather nice OSX cursors results in losing the little plus or shortcut arrow you get when dragging files around, so you have to guess whether or not explorer will copy the file, move the file or create a shortcut to it.

While this does add a certain zest to file operations, you can always do what I do, and drag with the right mouse button.


Your iPod might not be as dead as you think...

by Matt 24. July 2006 17:17

I'm just back from holiday. It was very nice, thanks for asking. Brilliant weather, great food, smashing company.

And of course, like any self respecting young man about town, I spent the couple of weeks preceding the holiday filling up my iPod. 1400 songs later, and the day before I leave, the iPod gods laugh in my face, and I'm greeted with a constantly rebooting Apple logo when I try and turn it on. Gah. Perfect timing.

So, I'm a techy kind of guy, and since I don't own one of these T-shirts, I thought I'd have a go. I upgraded the software (good grief, I'm 0.0.2 versions out of date!) and still no joy. Right. Restore to factory settings, and we're back. I've lost all the music, but that's fine, iTunes has it. A (not so) quick sync later, and would you Adam and Eve it, it's bust again. Google time. In and out of service mode, disk mode, normal mode, several wipes and long syncs later, and it's still bust. Just rebooting, stuck on the Apple logo screen.

There's nothing else for it, I have to go to the professionals. I need to take it to the Apple store. And I do my homework, I look on the web site - what do I need to do to return something? Apparently nothing. Let's phone them and see. Ah, automated system that eventually tells me to check the web page that I'm at, and then disconnects me. Nice. (Not one of the options was to talk to a human being. Rubbish.) So, it looks like I'm good. I make the half hour trip to the shop, stand in line, and then get told I need to book in with the Concierge Service, which means that my appointment is in the three hours time. My blood pressure is really good at this point, by the way.

The annoying thing about the Concierge Service is that it's really good. Book an appointment time, come back and get your stuff fixed. Marvellous. The bad thing is that it's only good when you know about it. I actually went looking for something like this, and didn't find anything (I did actually see the Concierge Service link on the web page, but a concierge is someone who helps you out in a hotel by getting information, and handling stuff like getting you tickets. It's not a repair man. Poor name, Apple). This needs to be more discoverable.

Anyway, back to the whole iPod thing. Chappy takes a look at it, wipes it, chucks loads of files on, and it's fine. Bugger. The silver lining is that the iPod isn't broken. The cloud is that I probably have a dodgy music track. One in 1400. I think it's called needle.mp3.

So, here's the reason for this post - smart playlists are your friends. Firstly, make sure you've got your iPod sync options to just be checked files. Then uncheck all files, so nothing is being copied across. Then create a smart playlist such that the contents consist of all files beginning with "A". Highlight all (ctrl + A) and check them. Sync. Eject iPod. Check it works. Repeat with "B", "C", "D", etc. Sooner or later, the iPod will break. The last section you checked will contain a dodgy track.

Turns out my dodgy track had some funny data in the genre field. Shame on Apple for letting user data crash their device and waste my entire day. Still, a quick edit later, and my iPod is back in full working order. Just in time for holidays.

This story almost requires an ironic, "and I didn't listen to it once on holiday". Sorry to disappoint.


The Internet Provides...

by Matt 4. July 2006 07:16

It was only a matter of time. You can now download the new Vista Aero cursorsI mentioned the other day. Fantastic.

They're are actually rather nice. Very simple, and I think I'm actually growing to like the spinning circle busy animation, too.

Interestingly, (again, interesting if you're me) there is a new cursor that XP doesn't support - the alternate cursor.

Alternate aero cursor

This is the reversed cursor that gets displayed when you try and select a line in Word, or Visual Studio, and that always looks different to the cursor set you're currently running.

For now, I'm happy to use these instead of the Mac OS X cursors. High praise.


I heart MS blogs

by Matt 3. July 2006 18:12

I really like Microsoft blogs. They're a nice way of getting some insider information. I subscribe to a whole bunch of them.

The absurdly prolific Raymond Chen will teach you Programming for Grown Ups (aside: why buy a book that calls you an Idiot?). Jensen Harris will explain how Microsoft are innovating (and yes, I do mean innovating) on the user interface for Office. Larry and Eric have gone a bit quiet lately, but are still worth holding out for (even though Eric's gone a bit highbrow these days). It's nice to see Jeffrey Snover's obvious love for his Powershell (really, they should have left it "msh". Calling it Powershell just gives Slashdot something to laugh at). During one particularly interesting problem with IIS, David Wang became a bit of a hero in my team. And, looking at the comments, you just can't help but feel sorry for the Internet Explorer team.

These blogs are usually full of tips and information that just isn't in the official documentation. Sometimes it should be, other times, it's just how to join the dots to get it all working. They're a gold mine.

But it goes deeper than this. It's a two way thing. It opens Microsoft right up. Got a question? Find the team blog and ask! Post a comment, or use the form and send them an email (don't forget to be polite).

I've been working on a secret squirrel project to write a plugin to Windows Media Player for a certain well known brand of MP3 players, something that's not really been attempted by many people, so there's not a fat lot of information on the web. I got stuck, so fired off an email to Sean Alexander, who confirmed that what I was trying to do should at least be possible (and also wasn't for the faint of heart, but then, I already knew that).

And you never know, it might just have an actual impact on a shipping product.

I've been name-checked by the RSS team as one of a number of people trying to convince them to change the way the integrated RSS reader works in IE7, and they've changed it. (As it turns out, I'm not impressed with the solution. It's a bit all or nothing, and too easy to miss. A more fine grained solution would have pleased me more.)

But the best one is Scott Guthrie's Web Application Projects. A tip-top plugin for Visual Studio 2005, which brings back the sensible, pre-VS2005 web project structure, saving us poor souls who are struggling with the rather hobby-oriented default Web Site Projects. We downloaded and implemented this at work while migrating a rather large (i.e. the whole site) 1.1 project to 2.0. I particularly liked the way that it used partial classes to split the code you write and the designer generated controls into separate files. There was just one problem - the generated files didn't create XML comments, and we have those turned on, as well as treating warnings as errors. Which means that the plugin is generating code that doesn't compile. Dropped Scott an email via his blog, and bingo! a couple of hours later, I was included on an email trail from MS that showed it would be fixed and included in VS2005 SP1.

Which means that I (i.e, me, personally) am responsible for getting a feature into a shipping MS product. They added it for me. Because I asked them to. That's rather impressive.

Don't all thank me at once.

Of course, it could backfire. You could find yourself getting dissed by Raymond (admittedly, not the first time).



Month List


Comment RSS