Re: Emulating PTHREAD_MUTEX_RECURSIVE - recursive mutex implementation...
- From: "Mirek Fidler" <cxl@xxxxxxxxxx>
- Date: 12 Mar 2007 01:45:58 -0700
On Mar 12, 3:13 am, "Bin Chen" <binary.c...@xxxxxxxxx> wrote:
On Mar 11, 11:52 pm, "Mirek Fidler" <c...@xxxxxxxxxx> wrote:
On Mar 11, 1:54 pm, "peter koch" <peter.koch.lar...@xxxxxxxxx> wrote:
On 11 Mar., 08:38, "Mirek Fidler" <c...@xxxxxxxxxx> wrote:
On Mar 11, 7:54 am, "Chris Thomasson" <cris...@xxxxxxxxxxx> wrote:
http://groups.google.com/group/comp.programming.threads/msg/011401eb8...
sketch of a working solution...
Seems overly complicated and slow to me. Can you point out where are
the problems with mine solution first?
However, recursive is not really required
for anything... Think about it!
Yes, in theory, you are right.
However, I have just spent 4 days hunting down a bug which in the end
turned out to be non-recursive mutex deadlock - and that was not the
first time.
I think my solution comes practically for free, no expensive
operations are provided, so for my own sanity, I rather plan to use
recursive mutexes in the future.
Mirek
It might come from free, but it does encourage bad programming
practice. Always use non-recursive mutexes only. My "wrapper" over the
lightweight Windows mutex (CRITICALSECTION) checks that the mutex is
not locked recursively (when compiling in debug mode), and I believe
that this forces the users (primarily myself!) to implement cleaner
multithreading code.
Well, what you suggest is exactly what I was using up to this point -
altered windows critical section to disallow recursive locking. Still,
some errors obviously were left undetected.
As for "bad programming practice", I would not be so sure. I have
found non-recursive mutex to lead to more complex code in general.
Additional complexity is always a possible source of errors.
Although it is a digression, I want to know the extreme cases that you
must use recursive mutex to fulfill your design, I think many guys on
this groups also want to know. :)
"must" is not the same thing as "makes code simpler and less error
prone".
Of course, you can write all the code without recursive mutexes.
As for the case that took me 4 days of bug hunting:
In my memory allocator, there is a possibility to get memory profile
(memory allocation histogram). There is also possibility to record
"peak" memory profile - this gets recorded when allocator requires
more memory from the system. This option is normally off and gets
activated only occasianally. Even when activated, it not always
happens (because no more memory is required from system).
Now creating memory profile requires serialization, as well as
allocation itself (in case when more memory is required from system).
Which is deadlock.
Obviously, solution is to create locked and unlocked version of
getmemoryprofile. I have done this many times before. But in practice,
this can more than double number of functions dealing with serialized
data - and you still are prone to make a bug. You have to keep track
of the whole call tree of any serialized function and make sure that
only unlocked functions are called.
Mirek
.
- Follow-Ups:
- References:
- Emulating PTHREAD_MUTEX_RECURSIVE - recursive mutex implementation...
- From: Mirek Fidler
- Re: Emulating PTHREAD_MUTEX_RECURSIVE - recursive mutex implementation...
- From: Chris Thomasson
- Re: Emulating PTHREAD_MUTEX_RECURSIVE - recursive mutex implementation...
- From: Mirek Fidler
- Re: Emulating PTHREAD_MUTEX_RECURSIVE - recursive mutex implementation...
- From: peter koch
- Re: Emulating PTHREAD_MUTEX_RECURSIVE - recursive mutex implementation...
- From: Mirek Fidler
- Re: Emulating PTHREAD_MUTEX_RECURSIVE - recursive mutex implementation...
- From: Bin Chen
- Emulating PTHREAD_MUTEX_RECURSIVE - recursive mutex implementation...
- Prev by Date: Re: can a volatile int be used as a mutex]
- Next by Date: Re: Emulating PTHREAD_MUTEX_RECURSIVE - recursive mutex implementation...
- Previous by thread: Re: Emulating PTHREAD_MUTEX_RECURSIVE - recursive mutex implementation...
- Next by thread: Re: Emulating PTHREAD_MUTEX_RECURSIVE - recursive mutex implementation...
- Index(es):
Relevant Pages
|