inside this directory explaining what happens, and why (Hint: the result should not always be zero…). Then add, commit, and push the updated code and the results file, and verify that you can see the updated version on the web.
Shared Variable in Multithreading
Task 3
Behavior
In both the C and Go implementations, the shared variable i
is modified concurrently by two threads (or goroutines in Go). One increments i
by 1,000,000, and the other decrements it by the same amount.
Expected Result
One would expect to get 0
Actual Result
Lack of Synchronization makes the result unpredictable and vary each time it is ran. This is due to Race Conditions when the two threads try to access the variable at the same time.
Conclusion
The importance of proper synchronization becomes apparent, because without it we will get bugs.
Task 4
Mutex vs Semaphore
Mutex:
- Mutual exclusion. One thread at a time to access a shared resource. Simple. Semaphore:
- More general purpose, and more complex. Task can still be achieved with this, but with added complexity.