Is this example on condition variable correct ? | Sololearn: Learn to code for FREE!


Is this example on condition variable correct ?

Hi I was asked to print 1 to 10. Out of these numbers, thread one will print 1 to 3 and 8 to 10 where as thread two will print 4 to 7. Allso printing should be in ascending order. Is below code proper for this question?

4/27/2021 9:15:10 PM

Ketan Lalcheta

2 Answers

New Answer


The i variables may be pointless but yes that looks pretty good. The reason I say i is pointless is because you could be printing (_intCount - 1) and get the same output. Your loop conditions could be adjusted to things like while (_intCount<=8) for print2 instead of using a for-loop. Is the other variable just to prove that the mutex is working properly and that printing the sequence of integers isn't just a side effect of both threads sharing the same global variable?


Yes , I was trying to verify count... But definitely it can be avoided..thank you so much