Subj : Re: what shall we do when a lock fails To : comp.programming.threads From : David Butenhof Date : Wed Jan 12 2005 05:22 pm Joseph Seigh wrote: > On Sun, 09 Jan 2005 01:36:40 -0700, Andy Yang wrote: > >> Hi, >> >> I am pretty new to pthread programming. Currently I am reading David's >> Book, Programming with Posix Thread. In Chapter 7, page 261, David gives >> out an example on read/write lock. The code is as follows: >> [...] >> This segment of code return erronous status code back to the user, >> in case something goes wrong inside. Here my questions is, in this case, >> how shall we deal with the error when we use this read/write lock, >> or other similar locks that we build with pthreads routines? >> > > Abort or throw an exception. The most likely causes are incorrect use > or initialiation of the lock, or something clobbered the lock. There > is no meaningful runtime recovery in either case. Right. Normal mutex failure returns are serious application programming errors; not something that MOST code can elegantly handle and continue. (This is not true for recursive or error-check mutexes.) There's a tension in designing library code between "ease of use/simplicity" and "generalization". (And even more tension when the library code is specifically to illustrate techniques for a book!) The simplest and easiest-to-use path here would be to simply abort on an error rather than leave the caller to deal with it. It probably also makes the most straightforward example, but it also limits the error handling flexibility of the caller. For example, if the caller aborts it can report the file and line number of the call site, which might be relevant in determining what went wrong -- whereas the file and line of the library routine's call to pthread_mutex_lock() is usually pretty useless. But you could easily go either way on this decision, because it's somewhat arbitrary in this sort of context. -- Dave Butenhof, David.Butenhof@hp.com HP Utility Pricing software, POSIX thread consultant Manageability Solutions Lab (MSL), Hewlett-Packard Company 110 Spit Brook Road, ZK2/3-Q18, Nashua, NH 03062 .