Sign in to follow this  
wforl

Books on mulitcore/multi threading

Recommended Posts

I'm very interested in getting a descent understanding of the above topics, and was wondering if anyone had any books they'd recommend. There are a bunch of books available on Amazon, some very old and some very new, and so i thought I'd be better off getting some recommendations first. I would preferably like a book that comes with source code and plenty of examples of maybe the result with and without the multi-threading used. Im completely new to these concepts, and am not entirely sure what information you would need for recommendations, but i use C++ Microsoft visual studio 2008/05 win32 Directx these are the ones ive stumbled across Professional Multicore Programming: Design and Implementation for C++ Developers The Art of Multiprocessor Programming Multi-Core Programming: Increasing Performance Through Software Multi-Threading: Increasing Performance Through Software Multi-threading Real-Time Embedded Multithreading: Using ThreadX and ARM Intel Threading Building Blocks

Share this post


Link to post
Share on other sites
I quite liked Patterns for parallel programming for high level concerns. Though it lacks any treatment of the value of immutability and persistent data structures, which seems to me an incredibly powerful concept. The appendix acts as a nice introduction to OpenMP, though.

I don't think you can beat Butenhof's Programming with POSIX threads for the nitty gritty of using threads at a more primitive level. It's also largely applicable if you want to use the Boost threads library, since the concepts are taken from pthreads.

I've got a copy of The Art of Multiprocessor Programming, though I haven't found time to get in to it yet. The first few chapters start with some proofs and other foundational low level stuff. Later on there's talk of transactional memory and lock-free code, which is why I bought the book, but I haven't got there yet.

Share this post


Link to post
Share on other sites
In my experience books are great place to start, but mostly only cover the interfaces and functions. They never give practical or deep knowledge you need to avoid the minefield of bugs awaiting you. Here is what I have learned from 8 years of multithread programming in C++.

1. Anything written and read by multiple threads needs to be protected.
2. Do not acquire more than one lock at a time.
3. If you must acquire more than one lock always acquire those locks in the same order.
4. Do not iterate lists or maps with a lock and unlock in the loop. You should copy the list or the results you are looking for while locked then unlock and use the copy.
5. Only lock for as long as needed and no longer.
6. If something is read many times and written few times then use a Read/Write lock instead of a mutex or critical section.
7. Always create a class to manage the lock and another class to manage the locking of the lock. This way you never have to concern yourself with unlocking the lock. The locking class can acquire and release the lock in the constructor and destructor.
8. Keep your code as simple as possible. Lock do something and then unlock. I most often write functions to do each task the requires a lock than try a function that has to lock one thing release the lock and then acquire a different lock.Simple code makes it easy to track the state of the lock when debugging code.
9. If a class is allocated dynamically you must use reference counting. Have an int in the class that is incremented when a smart pointer class is assigned an object then decriment the count when the pointer is nulled or changed to a new object, when the reference count is 0 you can delete the object. The biggest crash inducer in multithread code is deallocating an object while its still in use by another thread.
10. Lastly do not store list or map iterators. Store the key value instead and refind the position the next time you use it. If the iterator points to a removed item bad stuff will happen.

notes
Mfc's lock classes suck! They never track the number of acquisitions made to make sure the lock is released appropriately.
windows does not have Read/Write locks but the behaviour can be simulated using an event and 2 mutexs.
Some versions of linux don't pay attention to the thread that has acquired a mutex so if the owning thread locks the mutex again it will be blocked.

Share this post


Link to post
Share on other sites
Quote:
Original post by david_watt78
In my experience books are great place to start, but mostly only cover the interfaces and functions. They never give practical or deep knowledge you need to avoid the minefield of bugs awaiting you. Here is what I have learned from 8 years of multithread programming in C++.


Some of these books will teach you about nice alternatives to locks, though! They seem to be the cause of a lot of your problems...

Quote:

1. Anything written and read by multiple threads needs to be protected.
2. Do not acquire more than one lock at a time.
3. If you must acquire more than one lock always acquire those locks in the same order.
4. Do not iterate lists or maps with a lock and unlock in the loop. You should copy the list or the results you are looking for while locked then unlock and use the copy.
5. Only lock for as long as needed and no longer.
6. If something is read many times and written few times then use a Read/Write lock instead of a mutex or critical section.
7. Always create a class to manage the lock and another class to manage the locking of the lock. This way you never have to concern yourself with unlocking the lock. The locking class can acquire and release the lock in the constructor and destructor.
8. Keep your code as simple as possible. Lock do something and then unlock. I most often write functions to do each task the requires a lock than try a function that has to lock one thing release the lock and then acquire a different lock.Simple code makes it easy to track the state of the lock when debugging code.
9. If a class is allocated dynamically you must use reference counting. Have an int in the class that is incremented when a smart pointer class is assigned an object then decriment the count when the pointer is nulled or changed to a new object, when the reference count is 0 you can delete the object. The biggest crash inducer in multithread code is deallocating an object while its still in use by another thread.
10. Lastly do not store list or map iterators. Store the key value instead and refind the position the next time you use it. If the iterator points to a removed item bad stuff will happen.

notes
Mfc's lock classes suck! They never track the number of acquisitions made to make sure the lock is released appropriately.
windows does not have Read/Write locks but the behaviour can be simulated using an event and 2 mutexs.
Some versions of linux don't pay attention to the thread that has acquired a mutex so if the owning thread locks the mutex again it will be blocked.


If lisp doesn't make you cross-eyed, check out some of the Clojure screencasts (for example) to see what you might be missing out on.

What I'm really saying is that I think it's important to choose a book or selection of books that teaches you about, or at least introduces you to, the world of concurrency beyond locks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this