Re: multi-threading fundementals?
runcyclexcski@yahoo.com wrote:
A possible explanation: Threads block each other (due to internal
synchronization) if a thread attempts any operation on a window created
in another thread. Review your code for any such cross-thread access to
windows/controls. Cross-thread parent-child relationships between
windows also have such issues. To get the performance you got with two
applications you have to limit each thread to touching its own windows
only, and these windows must be independent of windows created in other
threads.
The theads do operate on the same windows.
Does sending a message from thread 1 to thread 2 telling thread 2 to
update a window qualify as an independent operation? If not, I don't
know what to do, b/c I do need the threads to share windows.
I will post more details when I figure out a pattern. It seems like
simply initializing thread 2 (AfxCreateThread) w/o making thread 2 do
any CPU-intense taks (except for loading a window with controls and
have the window hanging there) slows down thread 1 significantly.
Here is a stupid question: is there any differnece performance-wise
between running two processes in two independent applications, and
running the same two processes in two indenendent threads within one
applicaiton?
"sending a message" is exactly the problem. Operations on windows use
SendMessage to the window. If thread 1 does a SendMessage to a window
created by thread 2 then thread 1 becomes suspended, waiting for thread
2 to process the message and return. Only then does SendMessage return
to thread 1. You have to avoid cross-thread SendMessage calls to get
the concurrent execution advantages of multithreading.
The solution is for thread 1 to PostMessage a user-defined message to a
thread 1 window, asking it to perform whatever updates are needed.
PostMessage will return quickly, without waiting for thread 2 to do
something.
There is little or no difference between running two processes and
running two threads. The operating system scheduler runs threads,
independently of whether they are in the same process or not.
--
Scott McPhillips [VC++ MVP]