Darling, Michael wrote: > Thanks for all the replies. I mistakenly convinced myself that an object > instance belonged to a particular thread as it would if it was in > another process (space). However I realise this is not the case. > > I am confused as to why you would pass messages to communicate between > threads though, could you not just update a central memory location? Or > is this because of the race condition? I think I just need to find a > real world example to put it into context.
Once you update that location, how does the thread know you've done it? It would need to poll that variable, looking for changes. It can't be reading from that variable while the sender is writing, or else it might read the wrong value, so you'd need to serialize access to that variable. What if more than one thread is interested in that variable's value? What if the thread that wrote the value wants to know when the receiver has finished whatever it's doing? Then _both_ threads need to poll variables. Most of these problems have already been solved. Ultimately, they all involve updating shared memory locations, but it's more sophisticated than what you'd probably do if you were building this from scratch. There are two ways of transmitting messages between threads. One is to send them. The sender calls SendMessage, and that function doesn't return until the receiver allows it. (There are even ways of returning a value before the receiver has actually finished handling the message.) Other other way is to post the message. The sender calls PostMessage (or PostThreadMessage) and the message gets added to the recipient's queue. The sender then continues on its way, not waiting for the message to get handled. This is useful when the sender either doesn't care about the result, or doesn't need to know the result immediately. Messages aren't the only way of communicating between threads. There are also critical sections, which are used for blocking access to a shared resource, such as a global variable. You also have semaphores, which allow a limited number of threads to manage a limited number of resources. (A critical section is like a semaphore that only allows one thread and one resource at a time.) Events are a way of notifying one or more threads that something has happened, but they carry no data payload of their own, the messages do. A mutex is another way of limiting access to a resource. Unlike a critical section, a mutex works across process boundaries. -- Rob _______________________________________________ Delphi mailing list -> [email protected] http://www.elists.org/mailman/listinfo/delphi

