Linked by Thom Holwerda on Fri 7th Sep 2007 19:38 UTC, submitted by koki
BeOS & Derivatives Haiku developer Stephan Assmus (Stippi) has posted the first in a series of articles on the topics of multithreaded applications. Stephan writes: "Though I am programming on BeOS since 1999, only in recent years I have slowly become more comfortable with various multithreading related issues in my programs. So I thought I'd like to share some of my experiences here for beginning programmers or programmers skeptical about multithreading. I hope to be extending this as a series of articles to help learn the benefits and pitfalls of multithreading. All with an emphasis on programming for Haiku's API."
Permalink for comment 269747
To read all comments associated with this story, please click here.
RE: Great but...
by averycfay on Sat 8th Sep 2007 05:27 UTC in reply to "Great but..."
averycfay
Member since:
2005-08-29

But for, ha, beginners, yep great article.


I actually disagree with that. While his example deadlock problem is fine, his solution to it kind of sucks. This won't make much sense to people who haven't read the article, but his solution:

- make Invalidate() pass a message instead of directly trying to notify the listeners (of the data in question)

I would solve it by:

- move Invalidate() 2 lines down, so that unlock() is called before Invalidate()

While his solution solves the problem, it introduces more complexity into the code and perhaps more importantly it adds yet another lock (lock for each message queue which he doesn't show in the article but would be required). He also doesn't follow his own advice of "hold a lock for as short of a time as possible". Invalidate() doesn't require holding the data lock so he shouldn't call it while holding the lock.

Here's my advice for programmers that are new to multithreading:

1) Hold locks for the absolute minimum time required
2) Minimize the number of locks (as performance will allow)
3) Don't use recursive mutexes. If your code requires a recursive mutex, you probably don't understand it well enough to debug it.

Reply Parent Bookmark Score: 1