Slashdot Mirror


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

7 of 69 comments (clear)

  1. But separate processes ARE better. by Kickasso · · Score: 3, Informative

    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?

    1. Re:But separate processes ARE better. by Blob+Pet · · Score: 4, Informative

      a pretty good summary that i agree with
      taken from http://basil.cs.uwp.edu/Cs370/notes/Threads.doc:

      Advantages (Thread vs. Process):
      -Much quicker to create a thread than a process.
      -Much quicker to switch between threads than to switch between processes.
      -Threads share data easily

      Disadvantages (Thread vs. Process):
      -No security between threads:
      ---One thread can stomp on another thread's data.
      -For threads which are supported by user thread package instead of the kernel:
      ---If one thread blocks, all threads in task block.

      --
      "...today consumers have been conditioned to think of beer when they see a bullfrog..."
    2. Re:But separate processes ARE better. by LunaticLeo · · Score: 3, Informative

      I must disagree with your advantages/disadvantages list. It was mostly accurate a decade ago and still true on some poorly designed operating systems (like Solaris).

      > Advantages (Thread vs. Process):
      > - Much quicker to create a thread than a process.
      "Much" is the nit I am picking here. The main difference between a process and a thread is that threads share the VM. Modern operating systems do Copy-On-Write (COW) for process creation. Hence, the diff between threads and processes is the creation of Page Table Entries (PTEs) in the process example (but that isn't as dramatically slow as copying the pages). Given that VM issues are the biggest performance issue in proccess creation versus thread creation, with COW you should only see minor to moderate speed hit for process creation versus thread creation.

      > -Much quicker to switch between threads than to switch between processes.
      That is again an implementation issue, and not true in the general case. Solaris makes context switching between processes slow compared to context switching between threads. Don't think of Solaris as a good example of a performance operating system (they have other things just not performance). An old benchmark comparison I saw, (circa 1998), it was lmbench running on Linux and Solaris 2.5.1 on the same UltraSparce hardware. The bottom line is that Linux's process context switching was faster than Solaris' thread (LWP) context switching.

      > -Threads share data easily

      Urr! I know you meant that it is easy because you can just pass pointers versus using some data sharing API like SysV shared memory. But I think threads are difficult to share memory _effectively_. Either you lock shared resources, or you get clever with thread safe algorithms/data structures. And that stuff gets real HARD real FAST.

      I just realized that you were including user space threading packages in your definition of threads. Yeah they can be pretty fast.

      If you want to expand your mind beyond this simpletons game of debating threads. Check out event driven systems, and co-routines. There are different trade offs for sure, but they are easier to debug than threads.

      --
      -- I am not a fanatic, I am a true believer.
  2. Right... by jasoncart · · Score: 1, Informative
  3. Re:Whats the Royal diff btw threads and processes by jeffy124 · · Score: 2, Informative

    Threads are considered "lightweight" processes, in that they run within a process concurrent to each other and the process that owns them. They share address space, making implementation of the alternative easier. Going with your 1-4 above:

    (1) Depends on what platform and how the threads are implemented. In some cases, you do see multiple entries in ps for a process running threads. Typically you see this in network deamons, but often that's because they use fork().

    (2) They've been around a very long time. Dijkstra (the guy who developed the shortest path algorithm for graphs) also developed a lot of stuff for threading in the 1960s. Though, fork() and exec() have probably been around longer.

    (3) Correct. Threads share memory. The trick is to make sure you eliminate race conditions so that one thread doesnt blow away what another thread is working on while it's working on it. There's two major ways of doing this: Semaphores and Monitors. pthreads use semaphores because they're a little easier to work with in C, whereas monitors generally need support from the programming language (eg, Java implements monitors, but can add your own semaphores to Java code if you wish).

    (4) POSIX has a Threading specification. iirc, pthreads in one such implementation.

    Uses: Within an OS Kernel, in GUIs, server apps, less overhead than using fork() et al. Useful in massively parallel computing applications where a lot of data gets shared. Like everything else in programming, it really depends on what's being developed.

    --
    The One Rule Of Chess You'll Ever Need: Don't play someone who carries a kit in their bookbag.
  4. State machines are for macho programmer by tigersha · · Score: 3, Informative

    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
  5. some disadvantages... by Polo · · Score: 4, Informative

    You know, there are a lot of practical disadvantages to threads.

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