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.


Comments (11) -

best suv 2011
best suv 2011
7/20/2011 10:38:41 PM #

I've recently began a weblog, the data you provide on this site has helped me tremendously. Thanks for all your time & work.


Meggan Monas
Meggan Monas United States
1/15/2016 4:57:01 AM #

The dolphin saving you one is the best one!


Brandon Penhallurick
Brandon Penhallurick United States
1/15/2016 8:32:29 AM #

The dolphin saving you one is the best one!


Francisco Brixner
Francisco Brixner United States
1/15/2016 8:54:43 AM #

Congrats. And all of the items featured so far should be in the book.


solar deals
solar deals United States
2/5/2016 10:07:14 PM #

Pretty good article. You have a knowledgable review on this subject and I'll be subscribing to your RSS feed and will hope you will post again soon on similar subjects. But I am would like to know what your article sources for the post are? Thanks a lot


Nicky Benshoof
Nicky Benshoof United States
5/6/2016 12:22:20 AM #

TY a great deal for posting, it was amazingly educational and aided plenty.. Therefore nice in the direction of find out another person with some initial questions on this subject. Amazing A whole lot more.


Scott Weldeen
Scott Weldeen United States
7/20/2016 7:43:30 AM #

You have an accurate point of view, thanks!


Clair Krob
Clair Krob United States
10/1/2016 11:23:21 PM #

So I sold you on the need to have a Port Coquitlam android app development, Surrey mobile app development for your business. Apple seriously amuses me sometimes. iTan is such an app that i don't really know what to say to. In April 2013, Marketdata Enterprises, Inc published its biennial report (that means once every two year!) "The U.S. You should handle the salary negotiation in smart way.


american bully
american bully United States
3/22/2017 7:18:15 AM #

i really did like this post i hope this link can provide value to your content please keep up the good work and i look forward to reading more<a href="">american bullies</a>


American Bully
American Bully United States
5/28/2017 3:17:29 PM #

I took a look at this link sense i kept seeing it and i really did like the information that i found keep up the good work <a href="">amrican bully</a>


american bully
american bully United States
6/3/2017 7:56:41 AM #

this is a must see blog we found it really good <a href="">american bully</a>


Add comment

  • Comment
  • Preview


Month List


Comment RSS