Re: MFC, Threads, PostMessage, and Reentrant WindowProc
- From: "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
- Date: Fri, 02 Feb 2007 08:46:42 -0500
tim.howells@xxxxxxxxxxxxx wrote:
Help!!!! I am struggling trying to cope with thread problems in
a MFC app. Due to MFC not being thread-safe, I am using
PostMessage to dispatch processing to the main thread in
cases where I would otherwise run into problems - for example
when I actually want to draw something on the screen. This
generally seems to work, but I can get deadlocks sometimes
in the main thread for no apparent reason.
I am using Visual C++ and I can examine each thread in the
debugger when a deadlock occurs. There is no apparent
resource cycle - just a normal lock in the main thread following
on after a PostMessage from a worker thread.
Now I am wondering if there is a really fundamental problem here.
Is it safe to lock any resource in WindowProc or any function
call tracing back to WindowProc? Since WindowProc is reentrant
you could get a deadlock simply by locking a resource and then
running into the same lock again in a reentrant call to WindowProc.
I can see how you might get in trouble this way if you don't unlock shortly after you locked. Why not just lock, copy the resource, then unlock immediately? Or even better, avoid locking the main thread at all by passing the data with the message by copying it to heap and passing the pointer in wparam/lparam.
--
Scott McPhillips [VC++ MVP]
.
- Follow-Ups:
- Re: MFC, Threads, PostMessage, and Reentrant WindowProc
- From: tim . howells
- Re: MFC, Threads, PostMessage, and Reentrant WindowProc
- References:
- MFC, Threads, PostMessage, and Reentrant WindowProc
- From: tim . howells
- MFC, Threads, PostMessage, and Reentrant WindowProc
- Prev by Date: Re: what is the fast(est) way to calculate millisec time span in m
- Next by Date: Re: BUG with RES/SCRIPT/XP-SP2
- Previous by thread: Re: MFC, Threads, PostMessage, and Reentrant WindowProc
- Next by thread: Re: MFC, Threads, PostMessage, and Reentrant WindowProc
- Index(es):
Relevant Pages
|