|
Threads-Devel : |
From: Anthony Williams (anthony_at_[hidden])
Date: 2006-03-06 03:36:05
Roland Schwarz <roland.schwarz_at_[hidden]> writes:
> Anthony Williams wrote:
>
>>I'm just pointing out that on Windows, it is legal to destroy a locked
>>mutex.
>>
> Are you sure? I tried to find something about this in the documentation,
> but nothing found.
The closest I've actually seen is in the "Object Interfaces" section of the
platform SDK I referred to earlier:
"Occasionally, an object remains in memory after all object handles have been
closed. For example, a thread could create an event object and wait on the
event handle. While the thread is waiting, another thread could close the same
event object handle. The event object remains in memory, without any event
object handles, until the event object is set to the signaled state and the
wait operation is completed. At this time, the system removes the object from
memory."
>> Whether or not doing so constitutes a bug in the application is another
>>matter --- I suspect that it is in most cases.
>>
>>
> Hmm, lets assume thread A locked the mutex M.
> Now thread B CloseHandle(M).
> Futher assume A does not access the object that is
> protected by M (altough unlikely).
> Now how can thread A ever release the Lock?
> The CloseHandle was succesful, so it isn't accesible
> any more except thread A is holding a duplicate of M
> obtained by handle dup or such.
For named mutexes, you can reopen them if you know the name. Also, as you say,
someone might have used DuplicateHandle.
>>I would want it to be illegal to destroy a boost::mutex if a lock is held,
and
>>for the implementation to detect this, if possible.
>>
>>
> Here we agree I think.
> Do you have an idea how this could be achieved?
Not for Win32 Mutex objects. However, I've now checked in my
basic_checked_mutex implementation, which checks for recursive locks, for
unlocks by the wrong thread, for locks when it's destroyed, for destroying
when it's locked, and for errors returned from the Windows API.
boost::mutex now uses this variant if BOOST_USE_CHECKED_MUTEX is defined.
All this is on the thread-rewrite branch.
Anthony
-- Anthony Williams Software Developer Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk