Good Books On Programming With Threads?
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?"
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.
... 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 in respect to that topic.
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 (or the super short overview). The book we used is the Addison Wesley book (ISBN 0-201-63392-2). It was informative and comprehensive
As for Java, there was an O'Reilly book (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. 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 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.
My work here is dung.
Working with threads! Get it! BA-ZING! Sorry. I'm not a programmer. Clearly.
We figured out a long time ago that it's easier to elect seven judges than to elect 132 legislators.
Probably not what you're looking for, but Real World Haskell is soon to be released and has chapters on concurrent and multicore programming and software transactional memory. Even if you're not interested in Haskell per se, STM is kind of an interesting idea.
Pthreads, Java threads and .NET threads are implemented differently. If you need a good Java book, just pick up one of the "Core Java" books that covers threading in one of its chapters since Java threads aren't that complicated. That said, with Java applications (the platform I know pretty well), if you're doing "enterprise" development it's best to avoid using them and let the application server do its black magic for you.
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/
The MSDN documents on the System.Threading object tree are pretty good.
SJW: a person who perceives an injustice, and while correcting it, commits a greater injustice.
Concurrent Programming in Java 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.
http://www.albahari.com/threading/
This is by far the best book on C# threading and best of all it is free. It cleared up a lot of the nuances of threading for me, and I think it would help any programmer.
The Jacquard Loom involves programming of a sort, albeit without branching or computations. In that sense it's more like a translator, translating punches into patterns. Sort of like printf for the clothing industry.
Knowledge is how to play a game, intelligence is how to win, wisdom is knowing what game to play.
I highly recommend this book 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 was working under the hood and helped my code considerably.
I'd suggest at least reading the threads section in the store and see if it helps getting the gist of it.
First: Programming Erlang: Software for a Concurrent World
by Joe Armstrong
http://www.pragprog.com/titles/jaerlang/programming-erlang
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
Not specific to any programming language, but a very good introduction to the concepts and methods used developing distributed systems, as all multi-threaded programs are.
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(pdf).
Article also available in html if you click on the first computer.org link from google. Hmm, why does it work from google and not from slashot?
+1 Pity laugh
Any plan which depends on a fundamental change in human behavior is doomed from the start.
I don't know about you, but I always program better with my keyboard.
"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.
Loading...
Here's one I found useful: Multithreading Applications in Win32 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.
Colin Dean Go a year without DRM
We use Concurrency: State Models & Java Programs in the Concurrency course at RIT. It discusses issues with multiple thread and explains how to model them.
Very nice read too.
http://www.doc.ic.ac.uk/~jnm/book/
Some background in parallelism is helpful for mastering threads.
I learned from this book:
http://www.lindaspaces.com/book/
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
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 systems designed by people who were not used to multi-threading).
One of the best value for money books I have for Java.
PS: The concepts and lessons, being at a software design level, are easily portable to other languages.
From an applied point of view this may not help you so much, but it does have a good deal of coverage on proper usage. And plenty of examples.
Green Tea Press
Best book that I have found in > 15 years of programming and teaching threaded programming is "Concurrent Systems" by J. Beacon - she describes the really hard parts very well.
It's not the actual programming that is hard in threaded programming; it's figuring out how to structure your design and understanding what you can get out concurrency and where the pit falls are.
Best,
but I think you should program with computers, not threads.
Of course, threads have legitimate uses too - but always try to see if you can design for separate processes instead, before commiting to a threaded single process model. Google Chrome shows the way.
Thread programming is not hard. Every processor on the market can only do one thing at a time anyway - meaning, the processor itself is pretty much single threaded. Yeah, yeah, multi core and all that stuff, but until there's compiler that takes advantage of it and actually is smart enough to allocate threads to each core, it's a single thread core. So there!
Just imagine a time slice to do one task at a time. And then call the API required. That's all.
Imagine sweeping the floor and brushing your teeth. One sweep floor. One brush stroke. Semaphore to pee...
Joe Duffy (of the .NET Parallel Extensions team) has an excellent book due to be published very soon:
Concurrent Programming on Windows
For Windows, I would recommend the book by Jeffrey Richter. It gives you the API underpinnings that the languages use.
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/.
Some rules of thumb that I've found useful:
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 :)
This sig used to be really funny...
Also make sure to take a good look at programming with THREADLESS concurrency. This is based on a proven model that consists of lightweight processes that share no memory (therefore no deadlocks) and send/receive asynchronous immutable messages.
Erlang is famous for this. Scala (JVM) supports it, and I think there is something for Java (with clumsier syntax due to language limitations).
This model is being touted as an almost-solution to the challenges of multicore programming.
please, just dont program in threads. if you do, years from now you will realize that you shouldn't. threads are a terrible mistake
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!
Try the CSP theory of Hoare, you will not need any 'applied' book after that :-)
http://en.wikipedia.org/wiki/Communicating_Sequential_Processes
Andrew Birrell of DEC and Microsoft has a couple of great papers on threaded programming:
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!
Google. Finds answers much faster than reading.
you should get out more
Thread Time by Norton and DiPasquale is older, but pretty good.
Programming Erlang
you had me at #!
I found Little Book of Semaphores a good read.
If you also plan to be programing in C++ you should definitely learn to use Open MP (http://en.wikipedia.org/wiki/OpenMP). As for which books to read I'm not sure but there are a couple on Amazon to choose from.
Look, moderators, that's offtopic. Not flamebait, offtopic.
Flamebait: being an asshole/provoking others into being an asshole. eg "Linux threads suck ass."
Troll: posting shit in order to get a response by people who think you're serious. eg - "I've been a lunix users for years. I wish it had thread support. That's why I'm switching to vista. "
Offtopic: anything unrelated to threading.
Those who just say "don't use threads" are just saying "I don't know how to use threads or I haven't been careful when using them, so stay away from them." The same could be said about certain computer languages.
Specifically, you need to be VERY careful of what you do with threads:
* ALWAYS Lock shared resources.
* Be careful of what order you put the locks into, otherwise you can stumble upon deadlocks.
* NEVER, EVER, update the GUI from a secondary thread unless you really know what you're doing (hint: You don't know). Gui programming doesn't mix up well with multithread programming.
* Dealing with detached threads is a headache. Use joinable threads instead.
* If you're developing a high-performance, low-latency app (i.e. multimedia), you may need to research on lock-free and wait-free programming (not for the faint hearted, tho). But if you're just starting with thread programming, I don't recommend it.
* Also, start making simple threaded programs as exercises. Change the order of the locks to see how you can screw up.
For research, before purchasing a book, search Wikipedia for the following fundamental topics:
- mutex
- critical section
- condition variable
- semaphore (these are the four fundamental synchronization primitives)
- read/write lock
- memory barrier
- RAII (How to make your threads exception-safe)
- dining philosophers problem
- Priority inversion (a typical problem with multithreading)
- Deadlocks
- Concurrent programming
Also Google for:
- joinable threads
- detached threads
- thread execution barrier
Good luck!
I can suggest three books... But you've got to be able to read them all at the same time ;-)
I don't necessarily mean the language, but the principles therein, they can be used in any language. The main point is that it avoids low-level locking (mutexes) and ensuing contention by never sharing memory between threads ("processes" in the Erlang VM) which would require it. Instead, data is passed as messages between threads, which work like event handlers then. In languages like C, you can also model this non-shared data by simply using read-only shared data, when you make sure that data isn't modified, you don't need to copy it between the threads.
Other than that, in event-based environments (typically window-based programs) you can often replace threads with a timer, which fires an event with the desired frequency. If all you need is to do something repeatedly and sleep in between, that works pretty well and and completely without locking. If you need a worker thread to do intensive computations in the background, that is not the way though.
cheers
Uli
(Ulrich Eckhardt)
"The Little Book of Semaphores" by Allen B. Downey is available for free as a PDF from the author. While it doesn't cover platform specifics, it does cover the core concepts of concurrency and synchronization in both Python and C/PThreads.
Java Concurrency in Practice was written by the authors of the java.util.concurrent package. It's an excellent introduction to the problems of concurrency and explains how java.util.concurrent deals with those problems.
Fair warning: It's written using a lot of the Generics syntax, which may steepen the learning curve if you're not used to Generics.
Is people don't multitask very well - especially when trying to read from two places at once. So I don't think any book that uses a threaded design is a good book
Programming With Card Looms, Jacques de Vaucanson, The King's Press, 1745.
Weaving Technology, Joseph Jacquard, Colonies? What Colonies? Publishing, 1801.
there is only 1 good book for programming with threads. It only has 1 page which reads: "don't use threads. if concurrency is truly needed -- use serialized io model with IPC "
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
# for x in `find '.' -name "*.c" -print`; # do perl -pie "s/==/=/ig" $x; done
Threads are easy. It is only when you try to share data between them when you get into trouble. This is one of the fundamental concepts in the study of operating systems.
A good OS course would cover concurrency, shared resources, semaphores, monitors, etc.. I'm currently reading Operating Systems Concepts 8th edition in my OS course (http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470128720.html) and it covers all of this in detail. This book also looks at these concepts across different programming languages and operating systems.
I just finished a homework on shared resources and I like the concept of a monitor much more than a semaphore. (although a monitor can be implemented with semaphores) I've also been coding in ada a bit lately and they have this wonderful structure called a protected type which is guaranteed thread safe by the OS. I'm kinda sad that ada is a dying language.
...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!
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.
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
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
One I remember from university was the 'dinosaur' book, "Applied Operating Systems Concepts" by Ali Silberschwartz, Peter Galvin, and Greg Gagne.
Although not a book on thread programming per se I remember it had lots of discussion about threads, semaphores, mutexes, shared mem, resource locking etc and I think good understanding of these topics is essential before you dive into trying to write and debug multi-threaded programs!
Especially if you already have significant programming experience I guess you mainly want the theory anyway.
I think it had exercises around these topics using Java as the teaching language to demonstrate the concepts. (If I'm still thinking of the same book)
http://www.amazon.com/Applied-Operating-Concepts-Abraham-Silberschatz/dp/0471365084?tag=particculturf-20
is pretty much Java Concurrency in Practice. Great book
Many OS-es have heuristics for having a separate thread (typically a kernel thread) do read-ahead. Having that speeds up (in wall time) cat or grep.
In C# there's: Threading in C#. It's nice to read and has nice code samples to explain the topics. Oh...and the price is also nice!
My signature is in the cloud.
I can recommend the Little Book of Semaphores, which is freely available at http://www.greenteapress.com/semaphores/
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 by W. Richard Stevens. 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.
--- Often in error; never in doubt!
http://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601/ref=pd_bbs_4?ie=UTF8&s=books&qid=1223461483&sr=8-4
as Programming with Threats
or was it
Programming with Treats?
Programming with POSIX threads.
Matt
While TBB give you one option to write C++ code in OO style, Boost.Thread and the upcoming new standard provide a similar API. Although not yet available entirely, this book looks promising to me:
http://www.manning.com/williams/
What's inside:
* When and when not to use concurrency
* Concurrency and multi-threading in C++
* Concurrency support in the New Standard
* How to improve performance with concurrency
* How to manage concurrency
* Problems and solutions in sharing data
* Synchronization and why we need it
* Memory model details
Description (from the website):
With the new C++ Standard and Technical Report 2 (TR2), multi-threading is coming to C++ in a big way. There is a new memory model with support for multiple threads, along with a new multi-threading support library featuring low-level atomic operations, as well as basic thread launching and synchronization facilities. TR2 will provide higher-level synchronization facilities that allow for a much greater level of abstraction, and make programming multi-threaded applications simpler and safer.
C++ Concurrency in Action is the first book to show you how to take advantage of the new C++ Standard and TR2 to write robust multi-threaded applications in C++.
[...]
The following white paper was written by Andrew D. Birrell of Microsoft Research and it is free. If you are looking for an introduction to multithreaded programming in general OR especially if you are using C# then it will probably be worth your while.
An Introduction to Programming with C# Threads (PDF)
Parallel FX and PLINQ (Parallel LINQ) and in general functional programming (F#) with a minimization of mutable state is the future.
Doing this stuff manually with threads is passe.
Look up on Google on how to do a progress bar in Java -- you know, where you do some long process such as an install or a long file save, and you get this bar widget telling you that it is "50% complete."
So the Java noob posts to a forum, "I do a long calculation, and I update the progress bar widget, and nothing happens." The response is "you are blocking the event-dispatch thread (EDT) from updating the GUI, and the proper solution is a 'Swing worker thread class."
So you spin off a Swing worker thread to do the long calculation, and you properly use invokeLater() to synchronize with the EDT to make periodic updates of the progress bar, and the EDT is not blocked by your long calculation. So seriously, what problem does that solve. The progress bar is no longer blocking the EDT at all, but it needs to be blocking the EDT in some sense to prevent certain forms of reentrency. The big deal with GUIs is that they are "non-modal", but there are times when you want some kind of mode and some form of modal behavior. When you are doing the long calc, you may want to update the progress bar, you may even want to enable a Cancel button to bail, but you may want to suspend all other activities of the app until this calculation finishes.
The classic example is that you need to save a long file on exit from an app. So how does a Swing Worker help you at all? The offered solution is to initiate the Swing Worker in response to the Close-and-Save command, and then let the Swing Worker invokeLater() back on to the EDT to post the quit message when finished.
The Windows solution is to use something called a nested event loop. When you are doing the long calc or the long file operation, you run a PeekMessage followed by DispatchMessage loop to keep event processing going. The problem with that is still reentrancy -- if the GUI is active, you could issue another menu command that would mess with your long calc or save. One answer to that is to use the form of the PeekMessage to only process paint events and otherwise lock out the GUI. Another answer is to do this from a modal dialog with a progress meter and a Cancel button. You PeekMessage all events, but since you are modal to that dialog, the only allowed GUI actions are progress bar updates and selecting Cancel, so you don't have the reentrancy problem.
The Visual Basic folks have a DoEvents() function that runs such a nested event loop, but the rap on DoEvents() is that the GUI is still active and you have the reentrancy problem. Most VB developers probably don't think it is a problem because they never test for it by poking at the GUI while the long calc is in progress. But again, the solution is to DoEvents() while presenting a modal dialog with the progress bar and cancel button. Or use a message filter as is not available with VB .NET.
People complain that a nested event loop is evil, or at least that it kills the non-modal aspect of the GUI. That is the whole point, there are times when you want the GUI to go modal on you, and the modal dialogs themselves have nested event loops in them to pull that off. The nested event loop with the appropriate message filters or modal dialog to suppress GUI reentrancy is often the proper way to handle the long calc with progress bar situation given that one may not want to permit other GUI operations until the calc or file save is complete. This business of telling people to use a Swing Worker thread and to Tarzan-swinging-from-the-fines to chain from form close, to long save, to post quit message is a complete kludge.
Windows is Windows you say, and Java is reliant on the Swing Worker to do this. Not! There is an EventQueue class in Swing, and it has exact analogs to the Windows PeekMessage and Dispatch message calls. So you can implement a modal state with nested event loop in Java anyway, and this should be preferred to threads for many cases where such a modal state is required. So why is the EvenQueue class in Swing such a guarded secret that no one recommends its use? It has been 30+ years since PARC, and people still don't know how to write a proper GUI app.
I'd advice you to download Apache source code
http://download.nextag.com/apache/httpd/httpd-2.2.9.tar.bz2
Slashdot = Sarcasm