I have two threads, called Fun
and Boring
. Each thread takes the same read-only data and runs some calculations over it and returns a [0.0, 1.0]
result.
In the current setup, Fun
acts as the main thread. It gathers, prepares, and stores the data in a global variable; a memory barrier is used. It then signals (via an event) the Boring
thread that there is data to work on. Fun
and Boring
now both operate on the data.
Here is code which illustrates the set up:
struct Data
{
void* someData;
} g_data;
struct Results
{
float fun;
float boring;
} g_results;
Fun::Run()
{
g_data = somePointer; // Prepare data
// *memory barrier*
SetEvent(dataIsPrepared);
g_results.fun = FunCalculate(g_data);
WaitForSingleObject(boringComplete);
// Do something with g_results
}
Boring::Run()
{
WaitForSingleObject(dataIsPrepared);
g_results.boring = BoringCalculate(g_data);
// *memory barrier*
SetEvent(boringComplete);
}
Is there a "better" way to share data and results? Are there any red flags that stand out?
Thanks for any feedback.