You need to retest your conditions after a wait returns. So:
if (received < expecting) wait_condition.wait_for(lock, limit);
should be:
while (received < expecting) wait_condition.wait_for(lock, limit);
This is for two reasons:
1. Some other thread could be waiting too, and racing against you. So you need to recheck the condition via a while loop after the wait returns (and when you have regained the lock) to verify the condition is really met, and you didn't lose a race.
2. Historically OS wait functions could return prematurely (eg signals interrupting) so needed sanity checking. But I don't think this would still be true in the C++11 implementation... ?
You need to retest your conditions after a wait returns. So:
wait_condition .wait_for( lock, limit);
wait_condition .wait_for( lock, limit);
if (received < expecting)
should be:
while (received < expecting)
This is for two reasons:
1. Some other thread could be waiting too, and racing against you. So you need to recheck the condition via a while loop after the wait returns (and when you have regained the lock) to verify the condition is really met, and you didn't lose a race.
2. Historically OS wait functions could return prematurely (eg signals interrupting) so needed sanity checking. But I don't think this would still be true in the C++11 implementation... ?