Information for Managers - Understanding pthreads?
dnotj asks: "The boss (who is very technically astute) says: NO to using pthreads in any of our production applications. He wants us to do things the old fashion way (fork(), exec(), shared memory, etc). His reason for this is that he doesn't understand pthreads (by his own admission). Hence, he is limiting us to using methods and techniques that he understands. He is reasonable and would see our side (the developers) if presented with enough understanding in a satisfactory format. What I'm looking for are document technically detailed yet directed more towards management. Not something on the level of 'pthreads for Dummies', but more along the lines of 'pthreads for Managers'. Any suggestions? URLs or Books are fine."
Why would a manager need to know this ?
Secondly, if he wants to know it, and already understands fork/exec approache, it should be very easy for you developers to exlain what threads are, and the various synchronization primitives provided. Thats basically all he needs to know, the pthreads api in detail shouldn't concern him unless he wants to program.
What he might be concerned about is portability, remember pthreads are not fully supported on many platforms, and also there are many surprises on other platforms.
this chapter should be all he needs to know + portability issues.
You share just what you need to share , and keep everything else private. With threads all memory is common. Didn't they teach you to use protection, or something?
http://www.google.com/search?q=pthreads shows, for starters :
You said the manager was tech savvy - bit of an oxymoron, but Check here for a what/how/why tutorial on pthreads.
For you managers out there, it's good to listen to your developers and let them show you things. Sometimes when they wish to use a technology or method, it's for VERY good reason.
There are a lot of managers out there who distance themselves... so be happy they are learning the technology vs a 2 minute shpeel on why they should use something.
-
ping -f 255.255.255.255 # if only
I've done some reading on these and Ive come up with:
(1) Threads are in the same process, so you would see just 1 in ps ax.
(2) Threads are new. Not many apps use it and older linuxen dont either.
(3) Everything is shared btw threads.
(4) Threads are a part of POSIX standard
I know nothing else. I couldnt find the why and how of it, and what condition will warrant the use of threads but not processes. Can someone give a clear and convincing reason to use threads over processes??(despite the overhead of learning it, and some incompatibilities across ports)
"Give orange me give eat orange me eat orange give me eat orange give me you." -Nim Chimpsky
Yes, switching between different processes is usually slower than between threads in the same process. If this is your concern, you probably better off having exactly one thread/process per CPU.
Mutex lock is an excellent protection against deadlocks (you need to use it with shared memory anyway), but it's less useful against plain old fashioned core dumps.
... but really, what you have to do is tell the boss:
"I'm sorry boss, i love you and all, but i'm the engineer here, and decisions like these are none of your business. If you're sure it would be done better without pthreads, then fine, code up a testcase, and we'll see."
Stumbling in the dark
I hear slavering of jaws
Eaten by a grue.
By all accounts, if your perception is that:
- boss is technically astute
- boss hates pthreads cuz he doesn't understand `em
Then there's inherent contradictions.You need to learn how to talk to your boss more, listen more, and, after listening, patiently explain with about 2 viewgraphs of bulleted items, the key features of threads and processes, with an even-handed listing of their respective pros and cons.
Then let him make a decision. Tell him your opinion is that pthreads is a better choice for this project, but you'll go with whatever he decides. In turn, express your appreciation for him holding up whatever decision and supporting you whichever way you go.
It wouldn't hurt your case if you explained that you've programmed in pthreads before, are familiar with the pitfalls and have encountered them previously, and think they are outweighed by the advantages. Tell him that only if it's true, though:)
"Provided by the management for your protection."
I'm thinking that if you can't give your manager a brief synopsis, then maybe he is right and you shouldn't be using pthreads...
Good: "Pthreads for Dummies"
Better: "Pthreads for Managers"
Best: "Pthreads for the Mentally Retarded"
I haven't lost my mind!
It is backed up on disk...somewhere...
Could be your manager is making the right call? Obviously, the roles in your group aren't clear to me. In my team, I'm the lead programmer, and I'm pretty conservative about adopting tech.
I don't just need to understand a technology, I need a deep undertanding of its strengths and weaknesses before I can start including it in our programs in intelligent ways. In practice, it means that I experiment with new things before I'll let the team use them.
I applaud your boss for his decision. There are many disadvantages of using threads, that I'm more than sure your boss knows about. See e.g. this discussion for more detail.
Now, granted, some applications will also benefit enough from threads to outweigh those disadvantages. It is rare, but it happens.
What I'm looking for are document technically detailed yet directed more towards management. Not something on the level of 'pthreads for Dummies', but more along the lines of 'pthreads for Managers'. Any suggestions? URLs or Books are fine."
If you have a boss that already knows enough technical stuff to micromanage your project in this way, I'm sure he is capable of googling out a pthread introduction himself. What you should be focusing on is explaining exactly why your projects needs threads, and why fork()/exec() won't cut it. This may also result in you accepting his decision, but that is as it should be.
"A computer is a state machine. Threads are for people who can't program state machines." -- Alan Cox
If your manager is making limiting and consequential technical decisions becuase of his/her lack of knowledge on the subject should they be managing.
Sun Microsystems Press has the best book I've read on the subject. "Better" than the traditional methods is up to your design. If you are spawning MANY processes per minute, on some implementaions pthreads will be much faster. There are some other payoffs as well, but really it comes down to what you are really trying to do...
-- John
I mean, if you need to ask others then it means you don't understand pthreads either. No shame there, so please don't be offended. Now, with this in mind, why do you want to push this technology? To what ends young man, to what ends? I go with your conservative boss.
p.s. I'm a luddite that thinks hype = shyte.
Managers aren't always wrong.
Laugh, but it seems to be a common attitude that a manager is never right, and never will be.
Your manager is not there to code, he's there to keep all of you developers on task. Apparently, for some reason he needs to understand your code, in order to do his job to his satisfaction.
Actually, he may not NEED to understand pthreads at all. But I believe his approach is pretty smart in this case. Why should he allow you to adopt a new method, when you cannot explain it to him?
I wouldn't let you use pthreads either, if my paycheck depended on you getting your job done.
...
'pthreads for Dummies', 'pthreads for Pointy-Haired-Bosses'...
Repeal the DMCA!
may no be what you'r looking for, but gives a lot of insight into different ways of dealing with this (prosesses, thread etc) problem. from Dan Kegel The C10K problem
Acts@core.mailboks.com Acrux@core.mailboks.com Adam@core.mailboks.com Adar@core.mailboks.com Ada@core.mailboks.com
except the stack. That's pretty darn simple, and
probably all he needs to know.
However, he's probably right. Threads are horribly
abused in most applications. They create bloat
and spaghetti code opportunities that lead to
user dissatisfaction and unreasonable maintenance
costs.
Threads should be used if you need SMP scaling
for a I/O intensive application, or shared memory
decomposition of a problem space dominates the
structure of the code. Otherwise, small
is beautiful, and asynchronous I/O is far more
efficient and elegant (which means maintainable).
-I like my women like I like my tea: green-
The O'Reilly pthreads book is a pretty readable and quick introduction for someone who knows C -- it explains the concepts, the API, and locking etc problems you will encounter and how to solve them.
It introduces the classic models with code examples and good diagrams and explanations (boss/workers, consumer/producer, peer workers etc) but it doesn't go much into real program or algorithm design with multiple threads of execution however, which is of course another can of worms (but you're already dealing with that sort of problem since you've said he likes you to use fork()s).
YMMV, I have a fairly old edition (1994 or something)...
Dear slashdot. My company uses Linux. I've tried to explain to my technical manager (who is very astute) that I would really like to have Windows 2000 installed on my machine so I can play half life. He says "no" and that work computers are for work. Can I get a bunch of slashdot readers to email bomb him until I get windows 2000?
DDB (who's had to deal with entirely too many managers whose concept of "appropriate" or "proven" tech == "what I know & am comfortable with")
Life is like surrealism: if you have to have it explained to you, you can't afford it.
...to your manager:
1) cost savings short term
2) cost savings long term
Anything else is just typical geek elitism. "Hey, let's use foobar technology because it's, well, COOL!"
If you are developing on Linux the new threads implementation shall kick ass anyway, so it should become sort of moot.
It's 10 PM. Do you know if you're un-American?
Processes get you a number of advantages above.
God that is stupid. OK, let met put it another way.
Computer work with machine code. High level languages are for people who cannot program in machine code.
OK, Alan Cox gets my het off because DOES program in Machine code, and I have too, but the thing is, threads are easier to maintain than a state machine because usually the two things are separate while in a state machine the bloody states have a complicated interaction.
Obviously the model where you fork 10000 threads off to anything is not viable. But for many apps threads make it easier to maintain the thing and keep separate things separate. A UI which has a separate thread for the UI and a thread for the program for instance is a VASTLY better model to program in than the stupid Event based crap that you see everywhere. See Java Swing vs. Java AWT for an example. And definitely see BEOS for an example.
Thread reduce latency because a single process with a state machine has to wait in blocking IO while a thread can go on. See Netscape that hangs in all windows because a TCP connect blocks in pone window for a good and extremely irreitating example. And Mozilla does the same. For that matter, IE too but in IE you can switch it off!
In general, if things are logically different things that run on their own (The UI vs the program that does something in the background) they belong in different threads. If you do not believe me, install BeOS one day ons a 200 Mhz machine and see how much more snappy it than Linux on your multi Ghz machine because the UI things (in their own thread) repond immediately even then the background thing that they control do not. State machines are simply to difficult to handle than seprate threads but because everryon uses them all our programs have godawful latency in the User Interface.
Also, one important thing. ALL computer programs that have an active GUI have two processors in it. The computer AND the user's brain. The part that the user controls is the GUI. That belongs in another thread.
And one last thing. Thread spawning on Linux is very, very good. Mucho better than under most Unixes AFAIK and therefore the "too much overhead" is crap. On Linux at least. And most program will get along just fine with 2-10 threads.
The dangers of excessive individualism are nothing compared to the oppressiveness of excessive collectivism
"A computer is a state machine. Threads are for people who can't program state machines." -- Alan Cox
You know, there are a lot of practical disadvantages to threads.
...except that the read thread is usually written with blocking reads and it's hard to recover when you want to kick that thread out of the read to terminate or restart cleanly.
Threads can be a lot harder to debug.
I don't think gdb really understands threads very well. Other application support is spotty as well. Actually, Solaris has pretty good thread-aware debugging and has good threading tools like deadlock tools.
People who write threaded applications also seem to think it makes life easier. They say - I'll just have a read thread and a write thread and a processing thread.
So people end up using select() anyway. And then they still need interprocess communication to kick the read thread out of the select sometimes.
I think you have to be a better programmer to use threads - you have to be aware of the normal issues plus all the threading issues. Avoid cancelling threads. You have to be very clear how you do signal handling. You have to lock all your data. Don't use fork+exec AND threads. You have to keep track of multiple threads, especially during error conditions.
And you have to be a much better debugger to debug someone else's threaded application.
Not that there aren't good applications for threads, just be aware of the complications.
Just a short rant on my experience and conclusions regarding threads. Me, having been formed in the "new age" school of programming (trained on the Commodore Amiga), always used threads. Used to design all my software with threads in mind. But not anymore. Since my jump to Unix, some five years ago, I've experienced a rather deep change of perception on this issue.
IMO, threaded code is much, much harder to implement correctly, and to debug, than single-threaded code. I used to blame the crappy thread support of many Unix libraries and tools. But now that gdb supports threads, along with pretty much everything else (except gprof, but the workaround is simple), I've found that it still is a PIA to debug all but the simplest threaded apps.
After years of hard work trying to determine what library calls are not reentrant, how are asynchronous signals delivered in each platform you support (God! Never, ever, ever mix threads and signals when writing portable code, not if you can avoid it), and which objects should be synchronized to get the best compromise between sync overhead and avoiding trashing the heap, you just fall in love with a design in which no locking is necessary at all. No ambiguities in library calls, no undocumented whatever_r() calls, no weird signal handling. A design in which a single core dump can tell you EXACTLY what was your program doing and why did it crash. Really, the simplicity and elegance of Unix programming without threads is just beautiful. The best adjective I can come up with is "liberating".
Of course, you do have to switch to a state-machine kind of mentality when designing your applications. It is not easy (was not easy for me, at least). But it can be done very cleanly and elegantly (for an example, look for the "State" pattern, the one in which you derive classes for representing your state machine states). If portability is not an issue, it can also be done very efficiently, if you use IO signals instead of select().
Now, having said all that, I'll now point out that I still use threads, because my code usually has to run on WIN32 too, and programming without threads there is hell (not that programming with them is much better). But I avoid threads as much as I can. I still worry about writing reentrant functions, because I find that to be a very good practice, even if no threads ae involved. But threads are used only when no other portable solution cuts it (like when checking asynchronusly for activity on non-socket descriptors, on WIN32).
One of my instructors has written a very clear and concise pthreads tutorial. You can find it in postscript, pdf, and LaTeX formats.
It gets right to the point and includes simple C code examples showing how to create and manipulate threads, mutex's, and condition variables. It's still a draft, but you might find it usefull.
Here is the introduction to the paper:
"This document is intended to be a short but useful tutorial on how to use POSIX threads (pthreads). In this document I do not attempt to give a full description of all pthread features. Instead I hope to give you enough information to use pthreads in a basic, yet effective way. Please refer to a text on pthreads for the more esoteric details of the standard.
"In addition to talking about the pthread interface itself, I also spend time in this document discussion some issues regarding thread programming in general. While not specific to pthreads, it is a must that you understand these issues if you are to use pthreads--or any thread library--effectively."
-73, de n1ywb
www.n1ywb.com
Not trying to flamebait here. But if your boss is really technically astute, he should be able to do the research, assess the pros and cons, and make a decision.
He should be able to pick up a book, google for some benchmarks, and talk to some people either face-to-face or on Usenet and then make a decision. A manager should be able to make this decision based on the facts over the weekend.
A very good manager would be able to make the decision in an afternoon. The real difference between good management and bad management is the rate at which you can make well-informed decision.
pipe/fork/exec is old school + way high overhead. Threads are thin + fast and will translate extremely well to parallel cpus (ie future architectures now that we (once again) can't make cpus faster and faster.) Getting experience with pthreads is good for longer term work. There are a few gotchas but you get used to them, mostly don't use statics within functions, make sure you clean up after threads exit (detach them), and of course don't leave any open files around or memory leaks. But there is a cleanliness to threads that fork/exec doesn't have (IMO).
His boss was tactfully saying "I do not trust my programmers' abilities well enough to implement this code using threads". He sounds like a wise man.
Threads suck. Fork() works much better. Don't forget, a fork() on linux is about as heavyweight as a thread on NT, and about THE SAME WEIGHT as a thread on linux. Linux threads are just clone()ed processes.
I see loads of Windows-programmers who have transitioned to Linux these days, and, to a man, they try and use threads. Fork() is so much cleaner, safer and neater for little or no performance hit (don't forget, local socket I/O is zero-copy on Linux, and shared mmap()ed files are, well, shared.)
Sun Tzu:
:).
"Do not oppose those with their backs to the wall."
"Leave an escape route for a surrounded army."
Maybe the boss really knows that pthreads aren't the way to solve the particular problems they have. I'm not that technically astute so I wouldn't use threads for a production app. But from the little I know, if I'm the boss I'll require someone _very_very_ good at pthreads to implement a production app.
So what if this guy doesn't seem good enough to the boss?
So he gives him a way to prove he is good enough (explain why pthreads is better to the boss), AND also a way to save face if he isn't.
Better than risking:
a) Misjudging this guy capabilities and thus create resentment and making him less useful.
OR
b) Correctly judging his current capabilities but in the process obliterating this guy's ego/confidence and thus potentially making him less useful to you.
The tradeoff is a little loss in respect at technical matters by claiming to not understand pthreads (do recall that the subordinate still does believe that the boss is very technically astute).
There might be a better way to handle such a scenario. But I'm going to sleep
You say Netscape/etc hangs in all windows when a TCP connect blocks in one window. That only happens if you are using Netscape with threads. Not if Netscape is in a separate process.
Your point that 2.5 different threaded browsers have that "one window blocks others" problem, seems to be more an example against using threads to me.
I think in most situations on Linux/FreeBSD it is best not to use threads. Use fork - default unshared memory, and then just explicitly share memory if necessary. Safer and easier to debug than "share everything".
And if one browser instance dies, it doesn't take the other 30 with it.
I just have to say that the poster here has my jealousy aimed squarely at him :) It is a very rare manager I have had that 1) had even the desire to understand the projects they managed (read: do their JOB) and 2) pragmatically and realistically admit their shortcomings. Of course number two, the astute reader will note, is in reality nothing but a manifestation of number 1.
Congratulations on having a manager who actually gives a crap. If only I could somehow bestow that care and concern upon the mindless snake-oil-salesmen we have, then that energy of them wandering around trolling up trouble instead of managing the people and projects under them would create a fantastic work environment with products we could be proud of. Who needs enemies when your officers are the ones causing more loss of ground and casaulties then any initiative by your enemies themselves?