I have three algorithms that I would like to process in a kind of pipline. All algorithms are to be executed endlessly, with continuous new data. The algorithm A creates a data set from a sensor algorithm B and C use this data set to derive events from it (B and C are independent of each other). However, all three algorithms take different lengths of time.
|AAAA AAAA AAAA AAAA
| BBBBBB BBBBBB BBBBBB
| C C C C
---------------------------
time
it is clear to me that algorithm B does not get every value of A and therefore only processes every second one sometimes. But that's not bad...
My idea would be something like this:
std::thread A_Thread;
std::thread B_Thread;
std::thread C_Thread;
while(true)
{
if (!A_Thread.joinable())
{
A_Thread = std::thread(&A::a, this);
}
if (!B_Thread.joinable())
{
B_Thread = std::thread(&B::b, this);
}
if (!C_Thread.joinable())
{
C_Thread = std::thread(&C::c, this);
}
}
My idea is: I check in an endless loop whether the thread is still working, if not I restart it. Within the individual threads I use a timestamp to check whether the currently available data has already been processed. A, B and C are methods that are located in the instance in which the threads are called. I guess I misunderstood joinable, because the threads unfortunately do not end themselves. So my first question would be how can I check if a thread grade is still working? I thought a thread ended itself when it reached the last one, but apparently not. Do I need a join for this? But then my endless loop no longer works ...
Another question is the variable exchange. i use instances within the threads where i save the results. the instance is in the instance (this) that calls the thread. is this possible? It only ever writes one thread to a variable, but everyone should be able to read.
is the approach reasonable or grossly negligent?