Solutions
These small exercises were mentioned in the What is a critical section? section.
Solution to Exercise 2. Again, ask yourself, what exactly constitutes a critical section? It’s when the code path in question both can possibly execute in parallel and works upon shared writable data. So, now, does the code in question (the lines between t1
and t2
) fulfill these two pre-conditions? Well, it can run in parallel (as explicitly stated), and it does work on shared writable data (the mydrv
variable is shared, as it’s a global variable, thus each and every thread within this code path will work upon that very same memory item in parallel).
So, the answer here is clearly yes, it is indeed a critical section. In other words, it should not be allowed to run without some kind of explicit protection.
Solution to Exercise 3. This one’s interesting; the second condition – does it work upon shared writable data? – is true, but the first condition...