Tim
Tim

Reputation: 99418

Who blocks who in blocking synchronization?

From Programming Language Pragmatics, by Scott

synchronization can be implemented either by spinning (also called busy-waiting) or by blocking.

In busy-wait synchronization, a thread runs a loop in which it keeps reevaluating some condition until that condition becomes true (e.g., until a message queue becomes nonempty or a shared variable attains a particular value)—presumably as a result of action in some other thread, running on some other core.

In blocking synchronization (also called scheduler-based synchronization), the waiting thread voluntarily relinquishes its core to some other thread. Before doing so, it leaves a note in some data structure associated with the synchronization condition. A thread that makes the condition true at some point in the future will find the note and take action to make the blocked thread run again.

Why is this synchronization mechanism called "blocking"?

Who blocks who?

Thanks.

Upvotes: 1

Views: 46

Answers (2)

GuangshengZuo
GuangshengZuo

Reputation: 4677

The Busy-waiting causes multiple unnecessary context switches as a process/thread repeatedly attempts to enter the critical section in a loop, so it costs much CPU time.

Blocking synchronization avoid this problem by having a process/thread block. It will be put into a waiting queue,instead of attempting to gain CPU time, the process/thread simply waits idly. No CPU cycles are wasted by a blocked process/thread, so other processes/threads can continue without unnecessarily sharing cycles. Once a critical section has been released by some other process/thread, something will wakeup the blocked process/thread.

So this is why this synchronization mechanism called blocking, it will be blocked or can not get cpu again util the lock is released by others.

Who blocks it? I would say the mechanism does it. It put the thread/process which not get the lock into a queue to wait and there is something like monitor to monitor the lock, once the lock is released, it will retrieve one from blocked queue.

Upvotes: 1

Solomon Slow
Solomon Slow

Reputation: 27115

Why is [it] called "blocking"?

Think of yourself driving to Grandma's house for thanksgiving dinner, when you come upon an accident scene: Tow truck operators are hooking up a big truck that's laying on its side, across the entire road. The traffic reporter on the radio says, "it's blocking both lanes." You might say that your way is blocked by the accident.

Who blocks who?

Like the text that you quoted says, It's voluntary. When you come upon the accident scene, you could just turn around and find a different way, but depending on how long the detour is, and on how soon it looks like they'll have the truck off to the side of the road, you might volunteer to sit and wait.

Software usually sits and waits. It's easier to write software that just waits, and its easier to to read and understand it. The kind of software that does not sit and wait is called a wait free algorithm, and they can be very tricky to write.

Upvotes: 1

Related Questions