Please create an account to participate in the Slashdot moderation system

 



Forgot your password?
typodupeerror
×
Programming

Good Books On Programming With Threads? 176

uneek writes "I have been programming for several years now in a variety of languages, most recently C#, Java, and Python. I have never had to use threads for a solution in the past. Recently I have been incorporating them more in my solutions for clients. I understand the theory behind them. However I am looking for a good book on programming threads from an applied point of view. I am looking for one or more texts that provide thorough coverage and provide meaningful exercises. Anyone have any ideas?"
This discussion has been archived. No new comments can be posted.

Good Books On Programming With Threads?

Comments Filter:
  • by eldavojohn ( 898314 ) * <eldavojohn@noSpAM.gmail.com> on Tuesday October 07, 2008 @12:55PM (#25288439) Journal

    However I am looking for a good book on programming threads from an applied point of view. I am looking for one or more texts that provide thorough coverage and provide meaningful exercises. Anyone have any ideas?

    I went through grad school not too long ago for Computer Science (disclaimer: it was the kind of computer science degree that doesn't focus on hardware so I might not be the best expert on this). Anyway there were two books for the class.

    One dealt with coding regular old C on a plain jain Unix machine and method of (I believe there are others) doing multithreaded in that environment is PThreads [llnl.gov] (or the super short overview [wikipedia.org]). The book we used is the Addison Wesley book (ISBN 0-201-63392-2) [amazon.com]. It was informative and comprehensive ... wasn't concentrated specifically on applications like you ask but very good reference. Also, I think there are a lot of good books free online [llnl.gov] in respect to that topic.

    As for Java, there was an O'Reilly book [oreilly.com] (there's probably a new version out for Java 6) that was pretty good. Not as great of a reference but better on applications of threads in Java. Although, as far as introductory material, I personally learned it all from java.sun.com [sun.com]. Although I can't vouch for whether this is an applied approach or not, I would suggest the concurrency tutorial and a good book on Java Patterns or even a design pattern wiki.

    I've never done concurrent programming in C# or Python so I do not know first hand what is best. I do know that erlang [erlang.org] has been fun to mess around with in my spare time though!

    Recently I have been incorporating them more in my solutions for clients.

    Most important rule of thumb of multi-threaded programming is to avoid it if possible. Maybe hardware (multi-core) will change that, maybe you feel the scheduler can't do its job as well as you can and maybe you feel it's more intuitive. But, often is the case, that you're just adding more complexity to your code resulting in more difficult bugs and harder maintenance for others. Keep it simple.

    • Re: (Score:2, Informative)

      by Anonymous Coward

      The Addison-Wesley book mentioned by the parent is "Programming with POSIX Threads" by David R. Butenhof. It's what I used when I needed to get up to speed on p-threads in a hurry - clear and easy to follow. P-threads are what's in Darwin, (and so BSD) Linux, and I'm guessing based on POSIX compliance, just about every commercial flavor of UNIX. (Presmuably, OpenServer uses fraying threads)

      • Re: (Score:3, Informative)

        by ByOhTek ( 1181381 )

        for the morbidly curious, there's even a pthreads library for windows. LGPLed

        http://sourceware.org/pthreads-win32/ [sourceware.org]

    • Re: (Score:3, Informative)

      by Anonymous Coward

      Most important rule of thumb of multi-threaded programming is to avoid it if possible. Maybe hardware (multi-core) will change that, maybe you feel the scheduler can't do its job as well as you can and maybe you feel it's more intuitive. But, often is the case, that you're just adding more complexity to your code resulting in more difficult bugs and harder maintenance for others. Keep it simple.

      Man, I have to disagree with you. That kind of dinosaur thinking will hold back progress. Multi-core is the future and multi-threaded apps are exactly what's needed to fully utilize its potential. I'm sorry if its too hard for you to debug but its just the way the cookie crumbles.

      • by zolaar ( 764683 ) on Tuesday October 07, 2008 @01:18PM (#25288771)

        Erm, the tenets of programming usually involve the general concept of "Eliminate the unnecessary." Therefore, the parent is correct: if multi-threaded processing is unnecessary, avoid it.

        What you meant to add to the dicussion is the corollary: If it is unavoidable, use it wisely.

        • Re: (Score:3, Informative)

          by greenbird ( 859670 ) *

          Erm, the tenets of programming usually involve the general concept of "Eliminate the unnecessary." Therefore, the parent is correct: if multi-threaded processing is unnecessary, avoid it.

          Although unnecessary, threading usually simplifies a program rather than adding complexity. The only caveat is that you understand threading. In my experience I've used threading to greatly reduce the size and complexity of solutions that either were or could have been implemented without them.

          • by Jack9 ( 11421 )

            Most programmers do not think in parallel processing terms, leading to claims that you should avoid it. While Amdahl and Gustafson have attempted to quantify the impact of strictly serial processes, the underlying premise is that parallelism is efficient (limited by serial processes).

        • by Gr8Apes ( 679165 )

          That's overly simplistic. Any interesting application of reasonable complexity can create gains of 100+% in performance with appropriately implemented concurrent design.

          That doesn't mean the performance gain is necessary, but it certainly is desired. Think of audio/video encoding, complex modeling, etc.

        • by refactored ( 260886 ) <cyent.xnet@co@nz> on Tuesday October 07, 2008 @04:43PM (#25291569) Homepage Journal
          1. You can avoid threads all you like... but several libraries / toolkits automagically spin threads for you. eg. If you using java graphical stuff odds on it has it's own thread whirring away doing stuff.
          2. Threads have subtle and noxious interactions with processes. Say "man pthread_atfork" sometime to see what I mean.
          3. ISRs/Timer/alarm/signal callbacks are effectively another thread context. ie. Most largish systems that claim to be "single threaded" aren't.
          • You're foolish if you do any real work in a signal handler; what you should do instead of send a simple message to the main event loop (traditionally by writing a byte to a pipe) and handle the event that generated the signal in normal program context.

            • ...not only is a signal handler another thread context, it's a dodgy thread context in which you may not do "real" work where the definition of "real" is some dodgy illspecified period of time. And then people wonder why systems aren't robust. Sigh!
      • I'm still not convinced effective concurrency requires changes to be made in software. I remember AMD working on something that would allow it to be done more or less automatically, even in a single threaded app. I don't know what happened to that or how effective it actually was, but I think the optimal solution is one that is transparent to the programmer, or at least nearly so.
        • Re: (Score:2, Insightful)

          by fitten ( 521191 )

          I'm pretty sure that stuff was some rumor that came out before Barcelona was released about how the Barcelona core was going to 'destroy' Core2 (basically a load of wild and crazy speculation).

          There's already some parallelisation of sequential code in all modern processors (out of order execution) that works well because it has fairly narrow focus on the instruction stream window. Going out further would be a much, much larger problem. Looking for parallelism in larger windows of the instruction stream, t

        • People have been working on automatic parallelization for decades. For a few specific areas it works pretty darned well -- for example, Cray had a Fortran compiler that could automatically vectorize some loops well over 20 years ago. It didn't always recognize code that could be executed in parallel (on its own) but it did so often enough to be quite useful -- it provided a noticeable speedup for a fair number of CPU-intensive processes. Around the same time, a number of other vendors had large parallel pro
      • Many people are scared of threads, because they try to do fancy things and get burned. The most important thing to remember about threads is KISS.

        Using threads can greatly simplify your programming, if you use them well. Keep your threads as modular as possible, like you would your processes. But unlike processes, you get to forget about IPC and all its problems. Woohoo!

      • by discord5 ( 798235 ) on Tuesday October 07, 2008 @03:00PM (#25290347)

        Multi-core is the future and multi-threaded apps are exactly what's needed to fully utilize its potential.

        For each application you name that is benefited by threading, someone else will be able to name one that isn't. Some processes simply are not parallelizable in a meaningful way, where meaningful is defined as in speed of execution not as in the interactive extravaganza of "looky how I can clicky the button while it's still doing hard maths".

        There's a good bit of reading about the subject, although much of it is boring and is often difficult to apply to real-world situations. Amdahl's law [wikipedia.org] in many situations can predict if it's worth bothering with multithreading (or other forms of parallelizing) quite easily.

        A tool like cat or grep has no benefit of being threaded since it's a simple sequential task. Suppose you were to multithread "cat" into one thread that reads from disk, and another that displays a line of text on the screen. Thread 1 will spend most of its time waiting for I/O, and thread 2 will spend most of its time waiting for thread 1 to pass data. Except now, your multithreaded cat has a somewhat complicated synchronization mechanism on top of it that makes it a bit harder to debug and probably eats some extra cycles as well.

        While the previous example is overly simple, there are plenty of tasks that are a lot more complicated but simply have no benefit of being threaded, because they spend more time waiting for I/O than actually calculating or because the algorithm is simply not worth parallelizing because there is no benefit in speed.

        Another example would be an application divided in 3 steps. Step A and B can be executed at the same time independently of each other, while step C depends on step A and B. Both step A and B can be written to use two threads, and if they'd use two threads they'd run in half the time of their non-threaded equivalent. On a dual core machine (or 2 CPU machine) running step A multi-threaded and then step B multi-threaded takes 1 hour. In the other case, running step A and at the same time (on the other core/CPU) running step B single threaded also takes 1 hour. At this point you gain nothing by threading. Of course here I assume that I/O by both processes at the same time doesn't create some sort of delay. But if you're working with large enough data sets (more than you can keep in memory) this becomes less and less of an issue since the I/O overhead will already be there anyway.

        If you add to that the fact that threading (especially synchronization) is a subject that is not well understood by everyone (in the "find me out of 200 programmers fresh from school, 10 who can write a program that benefits from multi-threading and actually works" sense), threading suddenly becomes less appealing if there aren't any clear benefits for the application you're working on.

        The reason I mention that last part is that because so many schools give kids the "make two threads count to 100 then exit" exercise but fail completely at getting the message across of the fact that most of the time the threads actually need to synchronize with each other. They'll give this long lecture about the dining philosophers problem without actually SHOWING them what that means.

        In conclusion: it depends on a lot of factors (size of your dataset, how well your algorithm can be split up in parallel tasks, ...) if your process benefits from threading or not, and you should evaluate at design time using Amdahl's law if there's an advantage or not. If your results in a multithreaded environment are only marginally better, the economical factor of cost of development time suddenly weighs in very heavily.

        Having said that: if you're a programmer, have fun with threads at least once. Write something silly in your spare time, it can be an amazing amount of fun and often offers an interesting way of approaching future problems.

        • by ShakaUVM ( 157947 ) on Tuesday October 07, 2008 @06:58PM (#25293345) Homepage Journal

          My Master's Degree was in High Performance Computing from UC San Diego, and I taught parallel processing.

          Yes, you're right that most new programmers out of college will screw up (and screw up badly) if they try to write a multithreaded application. Learning to write parallel applications requires more mind-bending mental gymnastics than, say, when you first learned to write recursive applications. That said, once you get a solid understanding of how safe parallel code should look like, and how it should work, it's fairly trivial to write code that works, and doesn't deadlock. From my experience, it takes about 3 to 6 months of pounding on parallel code to reach that state.

          While it's not a trivial amount of time, given the importance parallel code has (and will increasingly have in the future), I don't think it's too great a hurdle to ask for people to learn this stuff. All talk about multi-core programming always boils down to "Well, we'll never find enough programmers who are able to write multi-threaded apps." Well... why?

          I think it would be in the best interests of Intel and AMD to sponsor online college classes teaching how to do parallel coding. People aren't buying the new chips since code can't take advantage of it -- if they flip it around and make every program able to multithread (that could benefit from multithreading, as you point out, Amdahl's Law) then demand for their chips would surge, and they'd make the money back in billions.

          • All talk about multi-core programming always boils down to "Well, we'll never find enough programmers who are able to write multi-threaded apps." Well... why?

            1. Is multicore really the same as multithreading? I thought multithreading worked better with smaller compute cores sitting on one real 'core', rather than whole, separate cores, and that multicore was better for multiple concurrent processes, but maybe that leads into ...
            2. The lack of having them as first-class citizens in the O/S, at the level of recognition that processes have in the shell, the kernel, and programming/scripting languages -- e.g., listing them separately in /proc and other internal struc
            • >>he lack of a well-designed, intuitive multi-threaded debugger -- with how difficult multithreaded processes are to write, I can imagine this would be a very difficult task

              Yeah, the development environment, debugging, and language support for threads is kind of primitive. I've always just debugged my software using printfs - I've used various debuggers in the past, but I'm usually much faster at finding bugs by just printf-ing.

              >>Is multicore really the same as multithreading?

              Cores function like

      • Moore's law has gifted me with 2**20 increase in compute power since I started programming.

        But me old dinosaur brain hasn't been upgraded at all at all.

        Me (and my cow-orkers) were crappy at creating defect free concurrency when we started, and I'm afraid this new bunch of cow-orkers aren't much better either.

      • by jgrahn ( 181062 )

        Man, I have to disagree with you. That kind of dinosaur thinking will hold back progress. Multi-core is the future and multi-threaded apps are exactly what's needed to fully utilize its potential.

        It's funny how often the pro-concurrency crowd can come up with no better arguments than:

        1. It's the future!
        2. All processes must be able to use 100% of my CPU power!

        It doesn't match reality. Almost all processes are I/O bound, and will continue to be so unless networks, disks and keyboards see a sudden, major speed

    • I used the llnl.gov stuff to teach myself pthreadsy. I'll second that it is DEFINETLY a good resource, although it misses a few points. Those points are fairly obvious after looking through the pthreads include file with your system and the associated man pages for the functions. RWLocks are a good example.

      For C# I used the documentation in Visual Studio. Once you find the class reference, it's extremely useful and well done.

      For python, I just go to the python web site. They have three different sets of doc

      • by tepples ( 727027 )

        Maybe I'm crazy, but if you want a few tasks to be handled in parallel, how can the scheduler take care of it without threading? Forking?

        Yes. The current framework of computer science education makes interactions between processes much clearer than interactions between threads. And for years, Apache used processes to handle new requests because it started on platforms with efficient creation of processes.

    • by SQLGuru ( 980662 )

      I just got the latest issue of MSDN Magazine. There were quite a few articles related to multi-threaded (I haven't opened the plastic yet, so I'm not sure if they are good or not). http://msdn.microsoft.com/en-us/magazine/default.aspx [microsoft.com]

      Also hints that the next version of Visual Studio will be better tailored towards multi-threaded programming.

      Layne

    • Re: (Score:3, Informative)

      by ELProphet ( 909179 )

      Most important rule of thumb of multi-threaded programming is to avoid it if possible. Maybe hardware (multi-core) will change that, maybe you feel the scheduler can't do its job as well as you can and maybe you feel it's more intuitive. But, often is the case, that you're just adding more complexity to your code resulting in more difficult bugs and harder maintenance for others. Keep it simple.

      I'm going to have to disagree with you on this one. Especially in Java client side rich GUI apps, background threads are one of the most useful components to ensure a responsive interface when dealing with asynchronous requests. They really only need two and a half pieces to implement them easily and efficiently. The first component is the request itself, either a subclass of java.lang.Runnable or javax.swing.SwingWorker. The second is a callback handler. The half piece is the shared data structure, and it'

    • by hackus ( 159037 )

      MMmmmm...

      I take a different view.

      This is based on the fact that the clock increase dance we have enjoyed in speed is basically over now.

      The real increases in computing speed are going to come from the radical and the agressive use of threads everywhere in your programs.

      Why?

      Well, although speed is not going to increase, the number of cores per clock will increase dramatically over the future.

      Speed is going to come from doing everything at the same time on a per clock tick as much as possible.

      -Hack

    • by ozphx ( 1061292 )

      Its generally not possible to avoid threading in a .net project of even moderate complexity.

      * The UI pumps messages on its own thread. You need to do background processing on another thread.

      * Any communication should be done using asynchonous callbacks, or IO completion ports or similar. Otherwise you'll end up with the noob-esq while-sleep pattern.

      * ASP.Net pages run slow as hell, because people like to round trip to the database in sequence when databinding lists - this not only makes the pages slow, it l

  • Working with threads! Get it! BA-ZING! Sorry. I'm not a programmer. Clearly.
  • real world haskell (Score:3, Informative)

    by j1m+5n0w ( 749199 ) on Tuesday October 07, 2008 @01:01PM (#25288523) Homepage Journal
    Probably not what you're looking for, but Real World Haskell is soon to be released and has chapters on concurrent and multicore programming [realworldhaskell.org] and software transactional memory [realworldhaskell.org]. Even if you're not interested in Haskell per se, STM is kind of an interesting idea.
    • The problem is that functional languages are naturally FAR more suited to concurrent programming than procedural ones. To the point that I'm not really sure techniques for one are even applicable to the other. So my guess is that'll only be helpful if he happens to be using another functional language, which does not appear to be the case.
      • If you can't map functional concepts on to a procedural language, what good are you as a programmer?

  • Comment removed (Score:4, Informative)

    by account_deleted ( 4530225 ) on Tuesday October 07, 2008 @01:05PM (#25288571)
    Comment removed based on user account deletion
    • Re: (Score:3, Funny)

      by discord5 ( 798235 )

      if you're doing "enterprise" development it's best to avoid using them and let the application server do its black magic for you

      Finally, confirmation!!!! I always suspected all those acronyms to be some form of arcane hex.

    • Re: (Score:3, Informative)

      by Cyberax ( 705495 )

      On the contrary, Pthreads, Java threads and .NET threads are mostly the same thing in different packages.

      There are _really_ different ways to implement multithreading: fork-join model, pi-calculus, STM, message-passing model, etc.

      • by TheRaven64 ( 641858 ) on Tuesday October 07, 2008 @07:26PM (#25293625) Journal

        There are _really_ different ways to implement multithreading: fork-join model, pi-calculus, STM, message-passing model, etc.

        No, there are different ways of implementing concurrency. Threading, in particular, means shared-memory concurrency with a private control stack. Pi-calculus, STM, Linda and CSP are all examples of other models for concurrency, not of multithreading. They differ in many respects (although pi-calculus and CSP have a lot in common), but share one feature - they are all easier to reason about (and therefore to debug) than multithreading. The only valid use for multithreading is to provide an efficient implementation of one of the other models.

        • by Cyberax ( 705495 )

          Yes, thanks for correction.

          IMO, 'raw' multithreading should be used about as often as inline assembly. It's too low-level and error-prone.

  • by Deffexor ( 230167 ) on Tuesday October 07, 2008 @01:07PM (#25288599)

    I'm still getting the hang of Threading in C# myself, but I found this eBook immensely helpful in getting me understand some of the difficult issues such as Thread Safety, Cross-threading issues, Race Conditions, and Event-Delegate pairs.

    http://www.albahari.com/threading/ [albahari.com]

    • Hey this is pretty sweet, thanks! I just started learning C# and a bit of knowledge about threading would be great. :)
      -Taylor

    • ..aaaaaand i just realized that is from a book, and it was pretty helpful, so i bought the book!
      -Taylor

  • The MSDN documents on the System.Threading object tree are pretty good.
  • by progressnerd ( 1189895 ) on Tuesday October 07, 2008 @01:07PM (#25288609)
    Concurrent Programming in Java [oswego.edu] is more or less *the* book on good practices for multi-threaded programming for Java, with many lessons that apply to other languages as well.
  • by mckayc ( 307712 ) on Tuesday October 07, 2008 @01:19PM (#25288789)

    I highly recommend this book [javaconcur...actice.com] if you are doing threads or any sort of concurrent programming in Java. It's written by the guys who designed Java's concurrency features.

  • Before Brain was doing the HowStuffWorks podcast, he wrote what I consider to be the best book of how the low-level stuff in Windows works: Win32 System Services. I learned everything about threads in there.

    That said, it's specifically for C/C++ and never mentions Java or C#, so the examples probably won't help. What I really liked was how he explained the Dining Philosophers problem, how mutexes, semaphores, etc., work, and even though he talks a lot about specific API calls, I really learned how Windows w

  • Two books (Score:2, Interesting)

    First: Programming Erlang: Software for a Concurrent World
    by Joe Armstrong
    http://www.pragprog.com/titles/jaerlang/programming-erlang [pragprog.com]

    The Erlang programming language is well suited to develop concurrent programs with.

    The second book I'd recommend is
    Distributed Systems: Principles and Paradigms, 2/E
    by Andrew S. Tanenbaum
    http://www.pearsonhighered.com/educator/academic/product/0,,0132392275,00%2Ben-USS_01DBC.html [pearsonhighered.com]

    Not specific to any programming language, but a very good introduction to the concepts and methods u

  • by bugi ( 8479 ) on Tuesday October 07, 2008 @01:23PM (#25288843)

    The thread model has some fundamental problems, but since they seem here to stay there are some things you should keep in mind, nicely summarized in this article [berkeley.edu](pdf).

    Article also available in html [google.com] if you click on the first computer.org link from google. Hmm, why does it [computer.org] work from google and not from slashot?

    • Re: (Score:3, Informative)

      by TheRaven64 ( 641858 )
      Threads are a very good tool for building tools for building concurrent applications. They are not, themselves, a good tool for building concurrent applications and should not be treated as such. If you are building an application, stay away from using threads directly, and instead use a high-level concurrency API. If you are building a concurrency API, then by all means use threads (I have done, and so did the Erlang guys), but you probably don't want to be doing this just after reading a book on thread
  • "Multi-Threaded Programming Techniques" and "Win32 Multithreaded Programming" (don't worry that it says win32, the other book handles the non-Windows issues.) Together they make an excellent coverage of the topic. Later, get into the particulars of how threads are implemented on different platforms, it's quite interesting and really reflect fundamental differences in where to stress the performance of a system.

  • by GogglesPisano ( 199483 ) on Tuesday October 07, 2008 @01:35PM (#25289051)

    Here's one I found useful: Multithreading Applications in Win32 [amazon.com] by Jim Beveridge and Robert Wiener. It's a little dated (no coverage of .NET, for example - it's more focused on C/C++), but it still provides a good introduction to threading and synchronization on Windows.

    If you can find an inexpensive used copy, it's worth a read.

  • Howsabout books or sites on Python threaded programming? I'm going to be working on a project in a short while which will require the use of GTK and twisted together in a sort of network scanner system with asynchronous results.

    • What the hell? (Score:3, Interesting)

      by QuoteMstr ( 55051 )

      You don't need a book about threaded programming in Python.

      You need two books: one about Python, one about threads. Concepts are universal and can be applied across as many languages as you want. It's like saying you need to re-take Calculus because you just learned French!

    • I'm going to be working on a project in a short while which will require the use of GTK and twisted together in a sort of network scanner system with asynchronous results.

      You don't need threads for this problem. The issue is that you have two main loops (MainLoop/MainContext with GTK/GObject and the twisted reactor), you merely have to make them work together in a single event-driven loop.

      Use the iterate call in GObject's MainContext to run a single iteration using twisted's task.coiterate method. This way,

    • Re: (Score:3, Informative)

      by Vornzog ( 409419 )

      Howsabout books or sites on Python threaded programming? I'm going to be working on a project in a short while which will require the use of GTK and twisted together in a sort of network scanner system with asynchronous results.

      As much as I love Python, it does have some weak points, and threading is one of them. From the python documentation:

      The Python interpreter is not fully thread safe. In order to support multi-threaded Python programs, there's a global lock that must be held by the current thread before it can safely access Python objects.

      Threading is there, and I'm sure some decent documentation exists somewhere. But the GIL (global interpreter lock) generally means that there are better ways to approach the problem in python, i.e. processes instead of threads.

      It's a point of contention in the community, and the GVR-BDFL point of view is that any attempt to remove it makes Python a lot slower, so he won't.

      While I don't use

      • Re: (Score:2, Informative)

        by Anonymous Coward

        There is a new multiprocessing module 2.6 (and eventually 3.) that does allow for true multiprocessing. It uses an interface similar to but not exactly like threads, but actually is forking off new processes and using pipes to communicate in the background. It is possible to use shared memory between processes to give you all the benefits (and pitfalls) of threads.
        On systems where spawning processes is cheap (like Linux) I think it could be pretty useful. While certainly not as low-overhe

      • by rk ( 6314 ) *

        While I don't use twisted, I am given to understand that it does most of its asynchronous stuff using callbacks - you may be able to leave most of the concurrency to it and avoid the process all together...

        This is doable. I have some demo code I wrote to use twisted with pygame as sort of a proof of concept for a network playable game. It's not a real game, it's just flying a spaceship around a grid, and also provides a server you could telnet to and get updates on the position of ships and shots, but it's got enough code to show how to interface two event-driven systems together.

        If anyone is that interested to see it, write me at my username + my user id at gmail and I'll send it to you.

  • oldie but a goodie (Score:3, Informative)

    by fred fleenblat ( 463628 ) on Tuesday October 07, 2008 @01:37PM (#25289097) Homepage

    Some background in parallelism is helpful for mastering threads.
    I learned from this book:

    http://www.lindaspaces.com/book/ [lindaspaces.com]

    C-linda never caught on, but it's not hard to read the examples and apply them to pthreads, java, MPI or whatever framework you're using.

  • If what you want is to design multi-threaded applications (thus, more than just coding multi-threaded apps), then the book you want is: Concurrent Programming in Java [oswego.edu]

    A little on the stuffy side at times (not quite as easy a read as Design Patterns) it still provides a deep understanding of the trade-offs and techniques used when designing multi-threaded applications. Personally, I found myself again and again using the lessons and many of the patterns of that book when designing new systems (or fixing syste

  • Joe Duffy (of the .NET Parallel Extensions team) has an excellent book due to be published very soon:

    Concurrent Programming on Windows [amazon.com]

  • by EWIPlayer ( 881908 ) on Tuesday October 07, 2008 @02:08PM (#25289573)

    Herb Sutter has been doing a lot of work on this stuff over the last 10 years and his blog is full of stuff on what you should do... it's not too nitty gritty in terms of languages and stuff, but it's very informative in terms of understanding the issues and what not. Check out http://herbsutter.wordpress.com/ [wordpress.com].

    Some rules of thumb that I've found useful:

    • Hide mutexes and locks at (nearly) all costs. If you have a queue class, for example, that has a locking push() function, and someone needs to lock for a series of pushes, don't expose the lock to let them lock things for the series of pushes, but provide a push function that takes a list of items instead. Keep thinking of ways to hide your locking strategies. If your class is deadlock-free then you can be reasonably sure (I've always said "reasonably" but I've never seen it not work either) that you'll never see a deadlock in real life either. Race conditions are a different story, however.
    • Trying to figure out a solution where you never have to think about the concurrency of things is a scary place to go... Have a logical concurrent model instead. For example, if you work with user's and user's get events, rather than just letting them process any number of events in parallel, it may be reasonable to sequence events per-user and let the users run in parallel.
    • If you do have to expose locks, use a locking hierarchy. Herb shows this here: http://herbsutter.wordpress.com/2007/12/11/effective-concurrency-use-lock-hierarchies-to-avoid-deadlock/ [wordpress.com]
    • Avoid any concept of being impolite among your threads (i.e. forced interrupts or kills). Be polite. Herb has this here: http://herbsutter.wordpress.com/2008/04/10/effective-concurrency-interrupt-politely/ [wordpress.com]
    • Locking sucks, but it's necessary. If you think you can get away without having to lock in a dubious situation, you're probably wrong.
    • Unit test, unit test, unit test. If your classes hide all of your locks, then unit tests cover a ton of cases.

    I believe that following strict OO guidelines is even more important when dealing with concurrency than when dealing with general ideas in software... and let's face it, it's extremely important even when not dealing with concurrency :)

    • Re: (Score:3, Informative)

      by TheRaven64 ( 641858 )

      Locking sucks, but it's necessary. If you think you can get away without having to lock in a dubious situation, you're probably wrong.

      There are lots of good, reusable, lockless data structures around if you know where to look. Keir Fraser's PhD thesis contains a really nice lockless ring buffer design (which he implemented for Xen) and several other useful things (including a transactional list and some other shiny stuff). If you have implementations of these in a library somewhere, then you can often get away without locks. There is one rule you should always obey when writing parallel code though:

      No data may be aliased and mutable.

  • dont (Score:2, Flamebait)

    please, just dont program in threads. if you do, years from now you will realize that you shouldn't. threads are a terrible mistake

  • Google. Finds answers much faster than reading.
  • Use Erlang (Score:2, Informative)

    by toby ( 759 ) *
    Programming Erlang [pragprog.com]
  • the little book (Score:2, Interesting)

    by scoopr ( 849708 )
    I found Little Book of Semaphores [greenteapress.com] a good read.
  • by Troposphere ( 900433 ) on Tuesday October 07, 2008 @05:37PM (#25292281)
    I can suggest three books... But you've got to be able to read them all at the same time ;-)
  • by Jane Q. Public ( 1010737 ) on Tuesday October 07, 2008 @09:35PM (#25294629)
    Programming With Card Looms, Jacques de Vaucanson, The King's Press, 1745.

    Weaving Technology, Joseph Jacquard, Colonies? What Colonies? Publishing, 1801.
  • I took some classes taught by Allen Holub. Very smart guy, and I certainly enjoyed his book.

    He provides good solid explanation on functional models for queue design and listener patterns. He also discusses some pitfalls of threads in Java.

    http://www.holub.com/training/java.threads.html

    http://www.amazon.com/Taming-Java-Threads-Allen-Holub/dp/1893115100

  • by Secret Rabbit ( 914973 ) on Wednesday October 08, 2008 @12:39AM (#25295951) Journal

    That might seem wrong given that Python lists threading modules, but just look at Python's GIL to know what I mean. As in, no matter what you do, Python will still be running on one core. So, if you just want a performance boost because of a lot of I/O, then threads can get you there. Unfortunately, if you want to take advantage of a multi-core CPU with Python, Python's threads won't get you there. There has actually been a lot of discussion on this topic, but Guido just refuses to do it. The interpreter has no threads and the lib is not thread safe.

    If you want to do multi-processing with Python, look at its subprocess module.

    Guido's blog post on the GIL:
    http://www.artima.com/weblogs/viewpost.jsp?thread=214235 [artima.com]

    The FAQ entry on a (fallacious) reason why they won't remove it:
    http://www.python.org/doc/faq/library/#can-t-we-get-rid-of-the-global-interpreter-lock [python.org]

  • I can recommend the Little Book of Semaphores, which is freely available at http://www.greenteapress.com/semaphores/ [greenteapress.com]

    If by "an applied point of view" you mean "give me code that I can cut and paste into my app" this might be a bit theoretical for you, but if you make it through this book you will actually understand what's going on and recognize the difficult problems.

  • I learned threads from this book [amazon.com] by W. Richard Stevens. [salon.com] I have to warn you that Stevens' books are specific to C, but he has a clear and concise style, and his treatment of the basic issues such as synchronization, locking, shared memory, and semaphores is excellent. If you're coding in C, buy the book; if you're not coding in C you may not need to own it, but you ought to spend a couple hours reading Stevens' discussion of key issues.
  • Hoare's classic is freely available [usingcsp.com]. It's worth reading despite workable higher-level techniques being available. I like this bit...

    In its full generality, multithreading is an incredibly complex and error-prone technique, not to be recommended in any but the smallest programs. In excuse, we may plead that it was invented before the days of structured programming, when even FORTRAN was still considered to be a high-level programming language!

  • as Programming with Threats
    or was it
    Programming with Treats?

  • Programming with POSIX threads.

He has not acquired a fortune; the fortune has acquired him. -- Bion

Working...