Darling, Michael wrote:
> Am I right in thinking that, if you are using a separate thread to
> perform a process it should not call any code in the main thread?

It cannot call code in another thread. Function calls are always within 
the same thread. (That's part of what distinguishes one thread from 
another -- what context functions run in.)

A thread can send a message to a window that belongs to another thread. 
In that case, the sending thread will wait for the receiving thread to 
handle the message and return a result.

> I have a situation where a process is consuming a lot of time so have
> moved it into its own thread, however the problem is that the process
> requires a lot of the variables held on a datamodule attached to the
> main form. Some are plain variables others are calculated properties but
> all are static in that they are determined when the application starts
> and then do not change.
> 
> The question I have is, if I refer to the variables on the main form am
> I then tying the thread back into the main thread, thus removing any
> potential gain? I have only really played with threading in the past so
> don't have an instinctive feel for it yet. Also would the answer be
> different if just accessing say integers as opposed to calculated
> properties?

Variables are just memory locations. They don't belong to any threads.

Two threads reading from the same memory location simultaneously is 
safe. The unsafe thing is when you have one thread writing to a memory 
location and another thread reading or writing to that same location. 
Then you have a race condition and might end up with corrupted data. 
Protect against that situation with some form of synchronization. A 
critical section is a common technique.

Another technique is to simply make sure that you don't have any other 
threads active at the time you write to the variables you intend to 
share among multiple threads. That sounds like what you're doing. You're 
assigning values to all your data module's fields, and only after that 
do you initiate any other threads.

Functions are just memory, too. A function doesn't belong to a thread. 
Multiple threads can all call the same function at the same time without 
any inherent danger. The only danger comes if those functions try to 
access write to the same memory locations. But each invocation of the 
function gets its own local variables on its own stack. Each thread has 
its own private stack.

A calculated property is just a function.

-- 
Rob
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to