"LesleyAnne" <
XXXX@XXXXX.COM >writes:
Quote
OK, now I'm confused. The event handlers have to be members of the thread
where the component is.
Sorry. I was thinking that you were talking about GUI event handlers,
which must run in the main thread. Maybe I missed that you were using
some other type of event handler.
Quote
They need to use Synchronize to get anything to the main thread.
Yes, if they're running in the helper thread in the first place.
If you are somehow processing "events" in your worker thread already,
then you don't need a blocking message queue, since that's used for
communication between threads.
Quote
Even if there were some way to make a component in one thread use an
event handler in another thread,
There is such a way. That's what the message queue I described is for.
Quote
how would that other thread (my main application thread in this
case) then know which thread to send the command back to?
You could have a different message queue between each thread. Then
you know which thread you're talking to based on which message queue
you're using.
More typically, though, the main thread would tell the other threads
what to do, not the other way around (because the main thread isn't
waiting on a condition variable... it's processing the main
application event loop.)
Quote
The event handler's parameters don't include an indication of which
thread initiated the event.
What type of event handler are you talking about?
Quote
Then even dealing with commands that do start from the main thread, if it
simply called a member function of the thread to insert a command object
into the queue, how would that member function run?
When you say a "member function of the thread", that's somewhat
distorting reality. A thread isn't a class. It is a lightweight
process running in the same address of your application, with its own
stack frame and instruction pointer. A "thread object" is an attempt
to make an object LOOK like a thread, but really it simply is a normal
object that happens to also spawn a thread that runs a function that
is in that class. But OTHER threads can call member functions on this
thread-object, since it still is, afterall, just an object.
Quote
The thread is blocked until it sees the object in the queue, so how
can it be functioning to place it's own wakeup call there?
If you're using the message queue I posted, the push() function
signals the condition variable that the other thread is blocked upon.
That wakes up the sleeping thread, who then checks that the queue is
not empty (which it isn't), and so it breaks out of its wait-loop, and
dequeues the command and processes it. Once it is finished processing
that command, it calls pop() on the queue again, which blocks until
another message is available.
--
Chris (TeamB);
{smallsort}