Previous | Next --- Slide 36 of 60
Back to Lecture Thumbnails
crs

This process would also stall if we used lock1 for both threads, so in some ways atomic can act like a global lock (obviously not always)

gomi

What does synchronized mean here?

jchao01

@gomi, I think Kayvon answered in chat that synchronized essentially ensures that a lock is held while in the synchronized() block and released afterwards, and that a C++ implementation example of this is unique lock https://www.cplusplus.com/reference/mutex/unique_lock/

wkorbe

it's easy not to notice the lock1 versus lock2 here when quickly looking it over.

nassosterz

I believe that in all lectures, wherever synchronized is mentioned it can be thought as the c++ lock_guard, that locks when defined and unlocks when the scope where the lock_guard is defined exits.

nassosterz

Also, this is an example of deadlock, right?

parthiv

With atomic? Yes, I believe that would be deadlock. Both threads have "acquired" control of some subset of the necessary resources (i.e. 1 acquired A and 2 acquired B). They must hold these resources while waiting for the other resources. There is no way to preempt control of the resources since this would break the idea of an atomic block. And finally, there is circularity in the dependency graph, since 1 owns A and needs B, which is owned by 2 who needs A.

czh

Since the two threads uses different locks, isn’t deadlock possible with both the lock version and the version using atomic? If both threads use the same lock, there’s no deadlock with just these two threads, but there could be stalls and starvation if there are no other threads that update the flags without waiting on the same lock.

czh

Scratch my previous comment...I misread the true/false comparisons and got confused...there’s no dead lock with the original version using different locks.

fractal1729

I think this slide is a really helpful example that illustrates why locks are still pretty useful in a world with atomics. The core intuition seems to be that having multiple locks allows for a greater degree of flexibility than atomics; in particular, multiple locks allow for partial synchronization (along just each individual lock) while allowing non-atomic steps to happen in parallel.

Please log in to leave a comment.