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).


Oooh, new cursors!

by Matt 28. June 2006 03:00

Well, gosh. Microsoft have only gone and added new cursors to Vista. It's mentioned down at the bottom of the piece, 'cos, well, it's not exactly important news. Except of course to me. I've said it before (and I really must get that blog entry back up. If you're interested in a telling insight into my personality...) that the default Windows cursors are rubbish, and that you need to have animated feedback while the computer's off doing something - if only for the perception that it's busy, and hasn't hung.

Most cursors you can donload off the interweb are rubbish, but I've been lucky to find a set of Mac OS X cursors, which are really rather nice. The important thing is that the pointy cursors are bland and pointy, and the busy cursors are subtle enough to not distract you, while active enough to remind you something's happening. The OS X rainbow wheel cursors are great examples of this - simple, small pointy bits, and a nice touch of colour and animation when it's busy.


Microsoft's new cursors have nailed the pointy bit - subtly bland and nicely boring. They won't get in the way. I'm not convinced by the busy animations, though. They appear to use the same "busy circle" that IE7's been using, which is fine for a busy animation in an icon for a window, but I don't know how it's going to work on a cursor.

I'll have to find a copy and give it a whirl. It'll have to be rather special to make me leave my rainbow pinwheel, though...


Mottos. Who dares wins and all that.

by Matt 16. June 2006 18:43

For the last 18 months or so, I've tried to work to one of a number of mottos.

It started when I was writing a rather large piece of framework code. The motto was "make it easy for the caller". It meant that I would do everything in my power to enable the caller of my framework to write as little code as necessary, write as simple code as possible, and to actually get it right first time. I would favour making my implementation work harder, and writing more code in the framework over making the caller do it. Nice motto. I liked this one. Of course, it took a better man than me to sum it up in a nice pithy way - "the pit of success".

Once the framework was complete, I returned to working on normal, everyday client code. The first motto no longer held, so I pulled out a golden-oldie - "keep it simple". I think it's fairly obvious what this one's about; do not over complicate a solution. Do not over engineer it. Keep it simple. The guy who has to maintain your code in sixth months time will thank you for it.

The latest motto also comes via Brad Abrams. It's "sanity will prevail". Go read his description. It's a nice, upbeat way of looking at the world and coping with things that you disagree with, but are outside of your control.

Several months on, it's not quite as charming.

Right now, sanity is not prevailing. There's a distinct lack of common sense about the place, and everything just seems to be making life harder.

I'm kind of losing faith in this motto.

Now, maybe I'm just not giving it a fair crack of the whip. I know I'm not seeing any progress out of it, but I think it's really meant for the long haul.

Or maybe it's just not a good example of a motto. One of the dictionary definitions of motto is "a maxim adopted as a guide to one's conduct". This doesn't fit. This seems a better fit:

mantra: A sacred verbal formula repeated in prayer, meditation, or incantation, such as an invocation of a god, a magic spell, or a syllable or portion of scripture containing mystical potentialities.

So now I've rationalised my way out of that one, I need a new motto.


I hate the 24 hour clock

by Matt 2. June 2006 11:11

OK. Even I'll admit that this looks really petty, but let's face it - I've got a point.

Just look at this from a simple usability point of view. Nobody uses the 24 hour clock in Real Life. If I'm arranging to meet someone, it's never at "18 hundred hours" or "1815", but "6 o'clock" or "quarter past". I can tell if it's in the afternoon or not by checking to see if the sun is up (if it's 2 o'clock and dark, it's am. If it's light, pm. Easy.) I have to translate to 12 hour every time I look at it. I know it's not hard, but it's like reading in a foreign language - it gets in the way of what I'm trying to do; it makes me think about the format of the time, rather than what I'm doing, like checking to see if I'm late for an appointment.

Timetables are the only place I'm willing to concede that it's useful (and the military, well, they'll do what they want to anyway...). And then they're not doing anything that you couldn't do with "am" and "pm". In fact, I've started seeing timetables do just that, and they're much easier to read. And I don't understand why cinema's need to use the 24 hour clock either - they're not open at 8am, so you could tell just from the context what time the film starts. And using the 24 hour clock is just too formal - I only want to see a movie.

Now, I'm aware that this is just a cultural thing. A Frenchman I know used to jot down meeting times in 24 hour notation. It was natural to him - he used it. Here in the UK, we don't use it.

So, what does actually wind me up about all of this is that every video, radio, cooker, alarm clock or any other electrical item you can mention will have a 24 hour clock. These items are supposed to be labour saving, user friendly devices, and yet they don't even present the time to us in a format that we want. I'd really like to hear why a video designer chose to implement a 24 hour clock...

Even Windows gets in on the act - it's default time formatting for the UK is 24 hour. So even everyone's computer is adding to the trouble.

Petty? Perhaps. Poor usability? Oh yes. Bad design? Definitely. Know your target audience. Build appropriately.

(Or the alternative moral: if you're designing a new DVD player for the UK, please make it display the clock in 12 hour. You'll have at least one customer.)


Obsession With Detail

SUBv2. Nice...

by Matt 1. June 2006 10:47

I looked at quite a few blog engines before settling on this one. My needs were simple -, no SQL Server (I'm too tight), self hosted and I need to be able to extend it and add my own pages. That rules quite a few out straight away - all the big boys (MSN Spaces, Blogger, the .Text family of .Text, Community Server and SubText). In fact, a while ago, the only contender was DasBlog.

It ticks all the boxes; the data store is xml, so that handles my no SQL Server requirement. I just plain didn't like it. It's got a funky template system where each page is implemented as a single page that contains a single control which is a template processing engine used to generate pages. It seemed the wrong way round - is already a template processing engine that can generate pages, why write another one?

So, I decided (foolishly) to write my own. Didn't get round to it, obviously (far too many other interesting things to waste my spare time on) and so I was really rather pleased to come across Darren Neimke'sSingleUserBlog. This looked ideal. An xml data store, application, source code, sensible controls. Marvellous. All I had to do was upload it.

And change the CSS.

Now, I don't really like CSS, so managed to procrastinate long enough for Darren to release SUBv2, which was 2. It used Web Parts. Oooh, shiny.

And of course, my ISP was 1, so I got to procrastinate some more. But I upgraded a couple of weeks ago, and lo and behold, a lovely blog engine powered by the really nice SUBv2.

So, why am I telling you all of this?

Well, it's in way of warning.

I'm having great fun hacking the code base for SUBv2. Darren's done a great job, but having the source available means that I can't resist the urge to crank open the bonnet and have a root around. It's a great learning exercise for a lot of 2 stuff, and there were just enough things I needed to tweak before I could use it to get me hooked.

And the warning? Well, there's a lot of things I've got out of this that I'm going to post about...

But mainly, this post is just to say, thanks Darren. Nice job.



First Post 2.0

by Matt 25. May 2006 17:56

I think all blog systems should come with a first post already written for you. They're tricky beasts. You've got a bunch of things you want to say, but before you can get started on all of that, you've got to get the introductions out of the way. Cover the ground rules. And then there's the fact that there are no readers, either. Makes you terribly self conscious.

It's even more awkward the second time round. See, I've already written a first post, but that was back before I had a proper blog - you know, with a datastore and RSS and everything. About a year ago, I threw a page onto the internet, called it a "blog" and started writing "posts". It was all a bit rubbish, really, but it had a first post. Now, I've installed SUBv2 and I have a proper blog. I do intend to import at least some of those old "posts", but it's going to be a bit of a pain to do so, and there's things I'd like to post about in the meantime, and that means a first post. Again.

So here goes. Hello, my name is Matt. I'm going to talk about computers, because I like them, and it's what I do for a living. In case you're wondering why you should listen to me, I work as a Principal Software Engineer in a large internet bank in the UK. All day long, I talk about Enterprise-this and Design-Patterns-that and Agile-the-other. Many are the conversations I have about Test Driven Development, and the joys of C#. SOA's are my bread and butter.

Which means I'm not going to mention them here. There are much better people than me talking about all that stuff. Instead, I'm just going to post about the interesting stuff - all the cool little hobbyist type stuff that you'd really love to do, but can't earn a living from. It's kind of like an escape hatch.

Phew. Got that over with.



Month List


Comment RSS