views:

99

answers:

2

I am currently developing new features for an existing VCL application. The application creates charts and static images using a thirdparty package called TeeChart. There is one instance where I have to load in 2 million data points to create a static image chart. However, this takes a while to load and the user can't do anything in the application until it is completed. Therefore I would prefer to create a worker thread to process the data points so the GUI doesn't freeze.

The method setData() sets the following member variables, which the VCL component will then go on and use to create the Chart:

// Holds the Y position for the image (columns)
DynamicArray<double>* mpda_XValues;

// Holds the colour for the corresponding element in the x and y
// position
DynamicArray<double>* mpda_YValues;

// Holds the z position for the image (rows)
DynamicArray<double>* mpda_ZValues;

What things should I consider when creating a worker thread?

How might I create the thread using boost when all the data processing occurs in one method setData(){...}?

+1  A: 

You obviously will want to look at the boost thread library.

This is (probably) more complicated than you think. Converting a synchronous function that has side effects to an async function is non-trivial. Some things to consider:

  • Will callers of setData be able to handle the "setting" operation not being done when the function returns?
  • How will you ensure the background setData operation during shutdown, if one is on going?
  • How will you handle shared state between your GUI thread and the setData thread, as to not cause race conditions?
  • Will you need a "notification" mechanism, so the GUI thread can refresh (or do something) when the background setData completes?
  • What happens if someone calls setData while a background setData operation is pending?
Terry Mahaffey
+2  A: 

Since you are using the VCL, it might be a good idea to look at the TThread class. Create an inherited class from this, and use the Synchronize method when communicating with your main thread. You can try looking at: http://docwiki.embarcadero.com/VCL/en/Classes.TThread and http://docwiki.embarcadero.com/RADStudio/en/Defining_Thread_Objects

TommyA