In a multithreaded system, any order of operations could possibly happen, from each thread happenening serially, to all of them running at the exact same time. For example, if you have four threads, all 4 threads will execute (and inevitably finish) that same code flow. However, the threads do not necessarily start at the same time; the threads do not necessarily execute at the same rate (the code that each thread executes is not perfectly interleaved); the threads do not necessarily finish running the whole program at the same time.
Say we have the following RISC-V code to be run by multiple threads:
li t0 10
sw t0 0(s0)
lw t0 0(s0)
add t0 t0 t0
sw t0 0(s0)
Hint: Note that the memory location being accessed by each thread is the same (i.e. it is a shared variable). Think about the access pattern that would result to different threads reading a different data from the shared memory.
What is the smallest possible value stored at 0(s0) after two threads finish executing the code?
What is the largest possible value stored at 0(s0) after two threads finish executing the code?
Now, let's extend it further. What is the largest possible value stored at 0(s0) after four threads finish executing the code?