Mr. McPhilips,
Thanks.
(a) Instead of PostMessage() to update the UI from
the worker thread, can I call SendMessage()?
(this way I can avoid memory allocation and deletion)
(b) I am planning to keep all application data in the
document object. From an external thread, how can I
get access to the document object?
(I am not finding any method from AfxGetApp() or
AfxGetMainWnd() that returns the document object.)
I guess accessing the document object and providing
critical sections synchronization from the worker
threads is safe.
~~~~~~
. Reply (E-mail) Forward (E-mail)
Subject: Re: MFC Thread Safe?
3/29/2003 5:43:37 AM
Quote:
> I'm writing a multithreaded application. Half way through
> it, I read Jeff Prosise's MFC book and I was upset to
> learn that MFC is not thread safe.
> Then I reworked my program : worker threads now post
> messages to main window whenever they want to update the
> UI. But even to post messages to the main window, I
*have*
> to call AfxGetMainWnd() and/or AfxGetApp().
> (a). Is it safe to call these two functions in
> any worker threads?
> (b). Is there a way I can access objects that were
> created by primary thread, from worker threads,
> instead of posting messages?
> (any new mechanism is worked out...?)
(a) Yes, the two accessor functions are thread safe.
(Because they read
variables that never change.)
(b) Your threads can access data/objects that you create
in the main
thread, by passing a pointer to the object and providing
critical
section synchronization if the data can change. But you
cannot access
many MFC objects/functions that use the main thread's
message queue.
For example, you can access data that you store in a view,
but not the
MFC CView methods.
--
Scott McPhillips [VC++ MVP]
.