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."
Thread beginning with comment 269747
To view parent comment, click here.
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

RE[2]: Great but...
by anevilyak on Sat 8th Sep 2007 06:13 in reply to "RE: Great but..."
anevilyak Member since:
2005-09-14


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.


The message queues share the window lock in BeOS. Those are implicitly there as Lock A and B in his example, and are taken care of for you by the API in this case.

Edited 2007-09-08 06:14 UTC

Reply Parent Bookmark Score: 2

RE[3]: Great but...
by averycfay on Sat 8th Sep 2007 07:53 in reply to "RE[2]: Great but..."
averycfay Member since:
2005-08-29



The message queues share the window lock in BeOS. Those are implicitly there as Lock A and B in his example, and are taken care of for you by the API in this case.


I'm not familiar with the BeOS api, but if they are indeed the same locks as in his example (lock A and lock B), his proposed solution wouldn't even work and would deadlock too.

Reply Parent Bookmark Score: 1