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.

Tags:

Comments (80) -

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.

Reply

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

The dolphin saving you one is the best one!

Reply

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

The dolphin saving you one is the best one!

Reply

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.

Reply

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

Reply

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.

Reply

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

You have an accurate point of view, thanks!

Reply

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.

Reply

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="www.bonafideseo.com/.../American-Bully">american bullies</a>

Reply

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="https://www.bonafideseo.com">amrican bully</a>

Reply

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="https://www.bonafideseo.com/blog">american bully</a> https://www.bonafideseo.com/blog

Reply

Felix Lacko
Felix Lacko United States
8/17/2018 12:32:18 PM #

Don’t wear seat belts lest you drown in you own urine? https://php665.com/

Reply

Shakira Hackerd
Shakira Hackerd United States
8/17/2018 11:05:49 PM #

I have been checking out many of your stories and i can state pretty https://bzp65.com/

Reply

Denis Grabski
Denis Grabski United States
8/20/2018 5:27:25 AM #

of writing i am as well delighted to share my know-how here with colleagues. https://php665.com/

Reply

Rosella Hauk
Rosella Hauk United States
8/20/2018 9:40:34 AM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini https://bzp65.com/

Reply

Joya Rivard
Joya Rivard United States
8/20/2018 3:45:39 PM #

It?s hard to come by knowledgeable people on this subject, however, you https://php665.com/

Reply

Aurelio Sowers
Aurelio Sowers United States
8/29/2018 9:44:04 AM #

This is a impressive story. Thanks! https://newone2017.com/

Reply

Santos Washko
Santos Washko United States
8/29/2018 4:00:42 PM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Louis Shepeard
Louis Shepeard United States
8/30/2018 12:58:46 PM #

of writing i am as well delighted to share my know-how here with colleagues. https://newone2017.com/

Reply

Rosann Kusinski
Rosann Kusinski United States
8/31/2018 6:58:12 AM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Salina Ferguson
Salina Ferguson United States
9/1/2018 11:53:23 AM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Alda Elling
Alda Elling United States
9/1/2018 11:53:26 AM #

I got what you mean,saved to fav, very decent site. https://newone2017.com/

Reply

Adam Pfingsten
Adam Pfingsten United States
9/3/2018 6:11:09 PM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Thu Wibbens
Thu Wibbens United States
9/4/2018 4:51:05 PM #

Hello. impressive job. I did not anticipate this. https://newone2017.com/

Reply

Gilberto Froid
Gilberto Froid United States
9/5/2018 5:48:00 AM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Kayla Lagana
Kayla Lagana United States
9/6/2018 12:55:12 PM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Liana Burcin
Liana Burcin United States
9/6/2018 1:04:28 PM #

good stuff. I will make sure to bookmark your blog. https://newone2017.com/

Reply

Aron Coppock
Aron Coppock United States
9/8/2018 5:11:54 PM #

I have been checking out many of your stories and i can state pretty https://newone2017.com/

Reply

Nana Skutnik
Nana Skutnik United States
9/10/2018 12:02:26 PM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Madie Clery
Madie Clery United States
9/10/2018 5:27:00 PM #

Hello. impressive job. I did not anticipate this. https://newone2017.com/

Reply

Naomi Kruis
Naomi Kruis United States
9/11/2018 1:50:41 PM #

Black on black in the Charg I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Ardis Laisure
Ardis Laisure United States
9/12/2018 11:15:47 AM #

I have been checking out many of your stories and i can state pretty https://newone2017.com/

Reply

Morgan Monet
Morgan Monet United States
9/13/2018 9:39:41 PM #

sound like you know what you?re talking about! Thanks https://newone2017.com/

Reply

Brendon Capelli
Brendon Capelli United States
9/14/2018 10:46:11 AM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Phil Haskew
Phil Haskew United States
9/15/2018 1:59:20 PM #

With thanks! Valuable information!

Reply

Helen Schug
Helen Schug United States
9/15/2018 7:55:53 PM #

Don’t wear seat belts lest you drown in you own urine? https://newone2017.com/

Reply

Booker Stalter
Booker Stalter United States
9/17/2018 7:33:47 AM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Lyman Shamlin
Lyman Shamlin United States
9/17/2018 6:36:29 PM #

of writing i am as well delighted to share my know-how here with colleagues. https://newone2017.com/

Reply

Woodrow Tillett
Woodrow Tillett United States
9/20/2018 4:16:03 AM #

With thanks! Valuable information! https://newone2017.com/

Reply

Janetta Hall
Janetta Hall United States
9/21/2018 6:26:14 AM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Renaldo Priebe
Renaldo Priebe United States
9/22/2018 1:32:52 PM #

With thanks! Valuable information!

Reply

Louie Bacayo
Louie Bacayo United States
9/24/2018 9:03:53 PM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Dawna Grealish
Dawna Grealish United States
9/24/2018 9:09:50 PM #

With thanks! Valuable information!

Reply

Bernardo Diesi
Bernardo Diesi United States
9/24/2018 9:43:26 PM #

Black on black in the Charg I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

China Pierce
China Pierce United States
9/26/2018 5:08:23 PM #

Black on black in the Charg I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Andres Schellenger
Andres Schellenger United States
9/26/2018 5:08:30 PM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Rupert Karstens
Rupert Karstens United States
9/26/2018 5:09:57 PM #

Black on black in the Charg I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Gene Farwick
Gene Farwick United States
9/26/2018 6:12:06 PM #

Black on black in the Charg I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Xavier Lingerfelter
Xavier Lingerfelter United States
9/28/2018 7:43:47 PM #

With thanks! Valuable information!

Reply

Julene Remak
Julene Remak United States
9/28/2018 7:43:48 PM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Morgan Monet
Morgan Monet United States
9/30/2018 7:07:51 PM #

With thanks! Valuable information!

Reply

Brice Mostafavi
Brice Mostafavi United States
10/2/2018 2:11:02 PM #

Black on black in the Charg I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Elisha Strutz
Elisha Strutz United States
10/2/2018 2:11:04 PM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Santiago Whitbread
Santiago Whitbread United States
10/2/2018 3:45:11 PM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Yvonne Urness
Yvonne Urness United States
10/3/2018 7:54:49 PM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Susanna Whelihan
Susanna Whelihan United States
10/3/2018 7:54:52 PM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Bernetta Ferdin
Bernetta Ferdin United States
10/3/2018 9:25:06 PM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Lamar Beuchler
Lamar Beuchler United States
10/4/2018 12:26:06 AM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Earlie Krahenbuhl
Earlie Krahenbuhl United States
10/5/2018 6:55:14 PM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Jerilyn Dillaman
Jerilyn Dillaman United States
10/5/2018 6:55:14 PM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Paul Zapico
Paul Zapico United States
10/5/2018 6:55:16 PM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Bernie Yamkosumpa
Bernie Yamkosumpa United States
10/5/2018 8:56:12 PM #

Black on black in the Charg I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Responsive Website Design
Responsive Website Design United States
10/7/2018 12:55:11 PM #

每點擊付費

Reply

Tyler Bouldin
Tyler Bouldin United States
10/9/2018 8:20:41 PM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Micheal Yuill
Micheal Yuill United States
10/9/2018 8:20:44 PM #

With thanks! Valuable information!

Reply

Reginald Simich
Reginald Simich United States
10/9/2018 11:02:00 PM #

Don’t wear seat belts lest you drown in your own urine?

Reply

飛針
飛針 United States
10/10/2018 10:54:13 PM #

打造紋理細緻、水潤柔嫩和有彈性、晶瑩煥白的肌膚。 油性肌膚使用

Reply

潮人擦牙不用牙膏! 原來正在用這些口腔清潔用品    Marie Claire (HK) Edition  牙齒不夠白,任誰都沒有膽量露牙大笑吧? 雖然我們黃種人天生牙齒不夠白種人的來得亮白,但「美白牙齒」依然是大家一直追求的目標。 在香港,口腔清潔用品來來去去都是那幾個品牌,不是高露潔,就是獅王、Or    

Reply

IELLIOS是由歐盟資助倫敦大學細胞重建研究所研究. 採用諾貝爾生理醫學獎科技 , 透過"納米能量電流" 以最親膚與迅速導入的方式 , 利用電腦化系統去令皮膚再生 , 令皮膚組織在無創傷的情況下自然更新及收緊 . 這治療是無創無痛的 . 完成治療後亦沒有傷口 . 我們是香港第一引入IELLIOS的機構 , 醫生會根據客人不同情況去為你設計不同的組合 .在外國IELLIOS受到很多荷里活明星, 歌手以至政客的追棒 , Madonna的facialist kate somer -field就常用IELLIOS為她護理肌膚 , 令52歲的她肌膚輪廓均保持於30歲的狀態.  IELLIOS的訊號技術,採用心臟起博起原理,活躍無法正常運作的心臟細胞。訊號技術可活化及修復愛損皮膚,透過傳送訊號,激活靜止的細胞。IELLIOS的訊號技術給予細胞指令,引發細胞再次生長,令肌膚重回年輕。

Reply

Ciera Navo
Ciera Navo United States
10/14/2018 4:05:48 PM #

Black on black in the Charg I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Kathe Bierbrauer
Kathe Bierbrauer United States
10/14/2018 4:05:49 PM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Octavio Taper
Octavio Taper United States
10/14/2018 7:04:05 PM #

With thanks! Valuable information!

Reply

髮油
髮油 United States
10/17/2018 8:55:28 AM #

CLINIQUE 倩碧線上購物官網。瀏覽Clinique倩碧官方網站,了解更多線上購物、護膚、彩粧、香氛及禮品詳情。通過過敏性測試,百分百不含香料。

Reply

Lenard Justice
Lenard Justice United States
10/17/2018 9:30:17 AM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Stewart Auckerman
Stewart Auckerman United States
10/17/2018 9:30:19 AM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Cayla Sokolowski
Cayla Sokolowski United States
10/17/2018 12:10:46 PM #

Don’t wear seat belts lest you drown in you own urine?

Reply

KOJI 【眼部彩妝】甜心睫毛膏 - 纖長捲翹的商品介紹   KOJI,眼部彩妝,甜心睫毛膏 - 纖長捲翹       

Reply

Marcelino Juilfs
Marcelino Juilfs United States
11/7/2018 1:00:24 AM #

Excellent beat ! I would like to apprentice while you amend your website, how could i subscribe for a blog web site? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear concept

Reply

Rodrick Milfort
Rodrick Milfort United States
11/10/2018 9:27:30 PM #

There are certainly a variety of details like that to take into consideration. That is a great level to deliver up. I supply the ideas above as basic inspiration but clearly there are questions like the one you bring up where an important factor will be working in honest good faith. I don?t know if best practices have emerged around issues like that, but I am sure that your job is clearly recognized as a good game. Both girls and boys feel the affect of just a moment’s pleasure, for the rest of their lives.

Reply

Johnie Casavez
Johnie Casavez United States
11/11/2018 9:13:50 PM #

It’s really a great and useful piece of information. I am glad that you shared this helpful information with us. Please keep us up to date like this. Thanks for sharing.

Reply

Add comment

biuquote
  • Comment
  • Preview
Loading

Rel=Me

Month List

RecentComments

Comment RSS