+ 3

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? https://code.sololearn.com/ca8A12a1498a/?ref=app

27th Apr 2021, 9:15 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
+ 2
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?
28th Apr 2021, 1:21 AM
Josh Greig
Josh Greig - avatar
+ 1
Yes , I was trying to verify count... But definitely it can be avoided..thank you so much
28th Apr 2021, 5:09 AM
Ketan Lalcheta
Ketan Lalcheta - avatar