I am having some problems with a simple main thread + single worker thread application .. they may be due to my logic or perhaps to a misunderstanding of thread communication. I cannot reduce the problem to a simple example as yet, so I am looking for conceptual feedback.
I have a worker thread .. TworkerThread=class(Tthread). I want this to automatically FreeOnTerminate and it is convenient for me to have the thread tell another object (TWatcher=class(Tcomponent) ) about what it is doing, where it (the thread) has got to, what the status is. The watcher is owned by the form and survives the thread.
So I instantiate the Watcher in the caller (the VCL) and then set a property of the thread (eg property MyWatcher:Twatcher) to point to it
before resuming the thread. In the Execute method, MyWatcher properties (eg MyWatcher.CasesProcessed) are updated.
MyWatcher is only freed by the form, long after the thread has died.
There is only ONE worker thread per Watcher. Only the WorkerThread can update the Watcher and the main (VCL) thread only ever queries it.
My assumption here is that because the WorkerThread and the MainThread share the same address space, that whenever the WorkerThread alters a property of the Watcher, the updated Watcher properties will be available in the MainThread (whenever that gets switched to).
This is the crux of the problem. Can I be sure that any object that is instantiated in the main thread and updated in a worker thread (whch knows the pointer to that object) will be updated immediately? And hence will be accessible in its updated state to the worker thread and the main thread?
My architecture is, I think, simple. A MainThread, a Watcher instantiated in the MainThread and a pointer to that Watcher passed to the WorkerThread : the WorkerThread directly sets and gets properties of the Watcher, the MainThread examines them as needed.
looking for some elucidation here .. is my mental model wrong? can a thread update properties of an object it points to without issues of time delay and thread context switching?
I need conceptual help, and soon.