[CVALE] Threads on Linux

Pandora cvale at synx.dyndns.org
Wed Jun 28 13:04:46 PDT 2006


On Wed, 28 Jun 2006 11:58:18 -0700
"Landon Blake" <lblake at ksninc.com> wrote:

> I am looking for some information about programming with threads on
> Linux. I'm particularly looking for information on threads implemented
> in the c programming language on Linux. Also, I'd like to learn some
> more about the difference between threads and processes. I did some
> searching online, and read a couple of articles, but I was hoping you
> guys might have some other sources of information I could check out.

Sure, I've done a lot of threaded programming before.  Let me see what bookmarks I can pull up.

The LLNL has thankfully vastly improved their threads tutorial over the years:
http://www.llnl.gov/computing/tutorials/pthreads/

Here's some talk of the recent revolutionary development among Linux threads, that still works exactly the same way as before so you prolly don't need to read this:
http://kerneltrap.org/node/422

Before you go and use threads, make sure you aren't doing something that would be better handled by event based programming:
http://en.wikipedia.org/wiki/Event-driven_programming

Many threaded applications waste a lot of time locking and synchronizing, when they could have just lined up the thread events synchronously as event events.  Even when simultaneity is desired, as with multiprocessor environments, usually it's best to use an event-driven programming loop combined with a thread pool:
http://en.wikipedia.org/wiki/Thread_pool_pattern

Also don't necessarily discount the versatility of the good old fork() command, because it's supremely portable (except in Microsoft) and if you only have to fork very rarely such as in the prefork model Apache does by default:
http://httpd.apache.org/docs/2.0/mod/prefork.html

...it hardly produces any processing overhead at all compared to threads, since it only forks once, then reuses the forked process as HTTP requests come in.  Preforking and thread pools are just about identical matter of fact.

Oh one final thing.  Never use threading in perl.  Perl SCREWED UP their threading system. They have no soft threading model, nor even a hard threading model, in fact their threads are more expensive to produce than their forks since they decided to make it so that every variable in memory will be cloned and duplicated every time a new thread is created. The only time threading in perl is in any way possible is if you create all your threads at the beginning before any variables are declared, and in that case one might as well use prefork processing.

Warning: anyone can copy this email as it is public domain and not protected by any Orwellian laws. Everybody is allowed to disseminate, distribute it, and copy it, including the original author.
-- 
Pandora "Starling/Tasci/Antinomy/Figment/???" synx
jabber: http://synx.dyndns.org/jabber.png



More information about the cvale mailing list