I have one 'main' thread that creates an array of objects of class SlowData
. These objects are relatively slow to manipulate with. This is why, when I initially load them, they are filled with stub data.
Immediately after each object in the array is loaded, I can display the array in the UI. Parallel with that, I initiate the computation over the array. The SlowDataComputation
object is instructed to report results to the UI (which in turn will visualize the modification of a particular SlowData
object).
Here's how this would look:
void main()
{
std::vector<SlowData> aData;
aData.reserve(1000000); // one million instances
Fast_PopulateVector(aData);
DataUi aUi;
SlowDataComputation aCalc;
aCalc.SetUiCallback(aUi); // instruct the object to periodically
// report computation results to the UI
aCalc.Slow_ComputeInSeveralThreads(aData);
aUi.VisualizeInMessageLoop(aData); // message loop that exits upon user signal
if ( aCalc.StillWorking() )
aCalc.Stop(); // terminate threads gradually
// after VisualizeInMessageLoop returns, aData will then be destroyed
}
I want SlowDataComputation
to launch several threads, each of which would process a certain segment within the array:
class SlowDataComputation
{
std::vector<SlowData> * myData;
DataUi * myUI;
public:
void Slow_ComputeInSeveralThreads(std::vector<SlowData> & theV)
{
myData = &theV;
size_t aSize = myData->size();
size_t aNumThreads = 10;
size_t aBlockSize = aSize / aNumThreads;
for (size_ti = 0; i < aNumThreads; i++)
{
ComputationThread aThr(myData);
aThr.SetBoundary(i * aBlockSize, (i+1) * aBlockSize);
aThr.SetUiCallback(myUI);
aThr.Run(); // process a given segment in the array
}
}
};
Finally, here's some pseudo-code for the UI:
class DataUi
{
std::vector<SlowData> * myData;
public:
void VisualizeInMessageLoop()
{
Message aMsg;
while ( HasMessage(aMsg) )
if (aMsg.Code == Message::MSG_PROGRAM_QUIT)
SignalQuit(); // next time, HasMessage will return false
// return from message loop
}
};
The question is: how do I secure the destruction of SlowData
instances so that if any of the ten computation threads is currently working with an object, destruction needs to wait until each of these computation threads completes (either because it finished processing or because the SlowDataComputation::Stop
method was called)? What synchronization primitive is most suitable for this task?
I managed to solve this problem with one computation thread. I used Critical Sections. But this solution doesn't scale well for more than one computation threads.