Is CWnd::GetSafeHwnd() and CWnd::m_hWnd ThreadSafe?

CWnds are not mapped to HWNDs; HWNDs are mapped to CWnds, and this happens on a per-thread basis. The CWnd object is not in TLS (how would that work?) but temporary CWnd objects are created per-thread.

Accessing a temporary CWnd object from the wrong thread is definitely a bad idea (for the reasons described by Mark Ransom).

However, if you have a permanent CWnd object (representing the main window of your app, say) then, once it is created, there is no problem at all in accessing the m_hWnd member from any thread. It's just a value in memory that never changes.

If this troubles you (because it's not explicitly documented) then simply make a copy of the HWND and let the threads access that.

P.S. Here's the article you linked to in English.


GetSafeHwnd is simply a wrapper that checks if this is NULL, returns m_hWnd if not and NULL if it is. It won't be any more threadsafe than m_hWnd itself.

When you create a temporary CWnd*, MFC will destroy it at a point it considers safe, such as the next pass through the message loop. If you have multiple threads using MFC then your temporary object could get destroyed while you're still using it. Nothing you can do from your thread will detect this error.