MFC Thread Safe? 
Author Message
 MFC Thread Safe?

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]
.



Fri, 16 Sep 2005 06:17:03 GMT  
 MFC Thread Safe?

Quote:

> 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.

Yes, SendMessage can be used from the worker thread to update the UI.
The calling thread waits until the message has been processed, so make
sure the main thread will never be waiting for the worker thread or you
will get a deadlock.

When you create the worker thread you can pass a parameter to it.  This
parameter can be the doc pointer. You can also access the doc pointer
with CWinApp functions that enumerate all document templates and all
documents in each template.  Or, you could create a doc pointer member
variable in the app class.

--
Scott McPhillips [VC++ MVP]



Fri, 16 Sep 2005 13:35:16 GMT  
 MFC Thread Safe?


Quote:
> (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)

Watch out. I'm not sure which but SendMessage (or the other) will wait until
the message is processed before returning, the other won't. This can cause
problems if you hold a critical session on an object whilst sending the
message and the other thread needs the critical session to process the
message.

Quote:
> (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.

IMHO shy away from this whereever possible. I wanted to do that using CMap.
simply protecting it with a critical section/session (can't remember what
their called)
I had a problem in that the lookup function was fine when called from a
diffrent thread but other methods wern't.
In order to add objects in the end I created them and passed a pointer to
them to the other thread using postmessage, this then added them to the map.


Sat, 17 Sep 2005 03:48:08 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. MFC Thread Safe?

2. ANN: sigslot - C++ Portable, Thread-Safe, Type-Safe Signal/Slot Library

3. Thread Safe for CDatabase under MFC

4. Is the MFC message pump thread safe?

5. Is MFC 4.2 thread safe in object level?

6. Using a non-thread-safe library with threads?

7. Thread functions are thread-safe?

8. how do i create thread safe worker thread

9. MFC UI Thread doesnt start when COM method called in multi-threaded MFC dialog App

10. Is NetworkStream.Write() thread safe?

11. Why OleDbCommand objects are not thread safe?

12. Thread-safe DataTable.Rows.Add

 

 
Powered by phpBB® Forum Software