MFC Thread Safe? 
Author Message
 MFC Thread Safe?

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



Wed, 14 Sep 2005 10:59:27 GMT  
 MFC Thread Safe?

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]



Wed, 14 Sep 2005 14:43:37 GMT  
 MFC Thread Safe?
I too have read Jeff Prosise thread.
Use the lock outs.
You will find that if you create an MFC object in one thread it may assert
if you access it in a second.
Jeff says if it dosn't assert it will work. (Again as long as you use lock
outs)
Hope that helps
Rob


Fri, 16 Sep 2005 02:28:29 GMT  
 MFC Thread Safe?

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

MFC has "handle" map for each thread. That means, you cannot access CView object by  
handle in another thread. But if you have pointer to the MFC object, most of methods    
will work as excepted. But often you will need to use proper synchronization methods,  
and the most of methods will need calling thread running in message queue cycle [while  
GetMessage(...) etc..] (because, these methods using SendMessage function - this is    
general restriction in WinAPI).

Bredy



Fri, 16 Sep 2005 14:26:03 GMT  
 
 [ 4 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