Are Win32 windows thread safe?
I'd like to create a window on one thread, and then have that HWND migrate to one of any number of threads on which it will execute. My 开发者_开发知识库program will have multiple such windows. Is this possible or was the Win32 API not designed for this?
Yes, to a certain extent.
You can send and post messages to an HWND from any thread.
http://msdn.microsoft.com/en-us/library/ms644944(v=VS.85).aspx
Attempting to configure the UI (e.g. add controls) from another thread will end badly. However, if you send your window a message, you can be sure that the message will be processed on the creating thread.
No, this is not possible. The thread that a window uses for its message loop (what you refer to as "executing on") is defined at the time the window is created. You can create multiple threads and start message loops from them (and thus create windows on them), but this is generally regarded as dangerous.
There are two important calls that must be called from the same thread: CreateWindow
and GetMessage
(or their respective equivalents). Your solution wouldn't, so it's wrong.
You may call PostMessage
in any thread. SendMessage
is somewhat dangerous because it blocks in cross-thread scenario's, and could deadlock.
No, there is no concurrency checking on those calls.
精彩评论