Double lock on wxMutex(wxMUTEX_DEFAULT) allowed?
Alexander Steffens
steffens at math.uni-bonn.de
Tue Oct 2 09:51:37 PDT 2007
Hello,
shouldn't it be possible to workaround that like following sketchcode
within wxMutex::TryLock in the non recursive case on MSW?
Or are I absolutely naive?
//----------------
wxMutexError
wxMutexMSW::TryLock() {
//first do MSW-stuff as before
wxMutexError error = TryLock_old();
if(wxMUTEX_NO_ERROR != error)
return error;
//now we are sure that no other thread than current thread
//is inside this part.
//because we are the current thread we can not be here twice.
//therefore we are alone even if TryLock_old is recursive.
//now we check if we *was* here with the current thread
//for an unlocked lock before. If yes would have a dead_lock
//if we think in the non-recursive way.
unsigned long thread_id = wxThread::GetCurrentId();
if(m_hmpLockedByThread.find( thread_id ) == m_hmpLockedByThread.end() {
//ok we are here for the first lock with this thread
m_hmpLockedByThread.insert(thread_id);
return wxMUTEX_NO_ERROR;
}
else {
return wxMUTEX_DEAD_LOCK;
}
}
//----------------
and in the header
//----------------
class wxMutexMSW {
//symbolically
wxHashSet<unsigned long> m_hmpLockedByThread;
...
}
//----------------
and in Unlock we haveto remove the current thread id.
I think it would be fine do something like that within wxMutex, else most users
have to handle the incompatible behaviour between some unixes and windows
in their app.
Alexander
More information about the wx-users
mailing list