3

This seems to be an easy task, I just don't know which way to start using OmniThreadLibrary:

I create a Task that does some processing in the background. The results are stored in fields of the task class and are continuously filled with new values.

Now the main thread wants to read these fields and display their values from time to time.
Therefore it needs to access these fields and make sure that they are not written to at these moments (Synchronize).

How can this be done with OmniThreadLibrary?

2 Answers 2

3

There's no direct support for owner/thread data sharing in the OTL, because all my multithreaded experience tells me that this is always a bad thing to do. (Agree, sometimes it is the only solution but still it's a bad thing.)

You should go with the second mghie's suggestion - create an (optionally interface-based) object and pass this object (or its interface) to the thread. Something like this:

sharedData := TSharedData.Create;
task := CreateTask(worker).SetParameter('shared', sharedData).Run;

worker:

sharedData := Task.Param['shared'].AsObject as TSharedData;

Another way to solve the problem would be to send a 'please send update' message to the task whenever the user presses the UpdateNow button. That task would then respond with an object containing current state. However, if the task performs a lengthy uninterruptable calculation this solution is not really appropriate and the shared state approach works better.

2

Check out the OTL test 23, which implements a background file search. The SetParameter() method is used to set the search properties, the Comm channel is used to transfer results back to the main thread. The communication is already thread-safe, you need not implement any further synchronization.

Edit:

If you don't want a push but a pull model then you can of course use standard synchronization tools: an object that has a critical section which is used in all accessors to protect data from concurrent access. This object could be the task object itself, or any third object that is created by the GUI thread and passed to the task by (again) calling SetParameter(). If you don't use an object but an interface pointer you will get more safety, as the order of destruction is no longer important, the object holding the data will only be destroyed once the last reference to the interface it implements is reset.

1
  • As I understand this demo, the task sends it's results back to the main thread, whenever it has new results. In my case, the main thread only needs to get the current values when the operator presses an update button, e.g. only every 1000th time a new value is created. It would be nicer if the main thread could read the values only when needed.
    – Holgerwa
    Jan 11, 2011 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.