0% found this document useful (0 votes)
10 views1 page

Thread Interleaving and Race Conditions

Uploaded by

Lin Cong
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Thread Interleaving and Race Conditions

Uploaded by

Lin Cong
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

I opened the file [Link] and searched for thread interleaving patterns.

For example, in my
run I saw:

Thread: 50, new value of count: 444053


Thread: 40, new value of count: 443981
Thread: 50, new value of count: 444058

Here the execution switched from Thread 50 → Thread 40 → Thread 50. This shows a
context switch, since the CPU stopped running one thread, scheduled another, and then went
back to the first one.

When I checked the values of count, I noticed that the numbers are not always sequential. For
example, it jumped backward (444053 → 443981) which indicates that two threads were
updating the shared variable shared_counter without mutual exclusion, leading to
inconsistent results. This confirms that the race condition happened and count was not updated
correctly.

You might also like