To give a bit of context, I have n threads receiving ints. These are coming in cycles (let's say we have a uint16, so after 65535 the next received number will be zero again). There is no known mapping from the respective number to the thread it will be received from, like even numbers are received from thread 1 and odd numbers from thread 2 as a simple example. Some threads can also receive more numbers than others (which is actually the reason why the blocking is neccessary) There can be "jumps", meaning the numbers are not received in a strict ascending order. After a number x is received, the next number, be it in the same or another thread, need not be x+1 but can also be less than x or greater than x+1. We can make assumptions about the size of these jumps though (upper bound is N/2 for N being the largest number possible).
To allow the following subsystem to process the numbers correctly we need to make sure they don't drift apart too far when received. The following example should do the trick:
struct thread_status {
bool block;
uint16_t last;
}
void *thread_worker(void *data)
{
struct thread_status *staus = (struct thread_status*)data;
while (1) {
if (status->block) {
if (status->last <= MIN(array_of_lasts_from_all_threads))
status->block = false;
else
continue;
}
status->last = receive_next();
if (status->last >= MIN(array_of_lasts_from_all_threads) + ALLOWED_DIFF)
status->block = true;
}
}
Now the actual question is, how would I implement the MIN function? Instead of always comparing to the array of last values, I could also save the minimum last value and just compare to it. Then I need to implement the comparison operator though.
Another example to be more precicse: If I only have ints from 0 to 15 and 3 threads.
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | | | |X|X| | |0| | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Here, X are the last read values from the other threads and 0 os the currently read value. MIN and comparison are easy here. However in this case
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|X|0| | | | | | | | | | | | | |X|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
the minimum value should be 15 and not zero.