0

why thread is not giving different result

Hi As we all know, mutex should be used across critical section...Without this, thread might give different result every time we execute as some addition is missed in below sample code: https://code.sololearn.com/ca19A23A22A5 but for each run, same result is observed... Am i doing something wrong? I thought to implement summation, but without using atomic and mutex, code is working fine... Any suggestion on why thread is not affected without critical section blocked by mutex?

26th Jan 2021, 7:16 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 2
I don't see anything wrong with your code. The loop might be finishing too fast or you're just unlucky. If you want to see it change more often add a small wait in the loop this_thread::sleep_for(10ns) or make the loop last longer by setting the ranges from 1 to 100000 and 100001 to 199999. Remember to run it a few times and you might get lucky.
26th Jan 2021, 8:08 AM
jtrh
jtrh - avatar