Home > Mobile >  What will cpu do when a thread waiting for a mutex
What will cpu do when a thread waiting for a mutex

Time:03-10

I'm curious about the behavior of cpu during a thread waiting for a mutex. Now I can imagine two possibilities:

  1. The cpu stay on the current thread and check if the mutex had been unlocked continually.
  2. The cpu will switch to another thread(or process) for a moment and switch back to the origin thread and check temporary. Which one is right or the stl implement in another way?

CodePudding user response:

In the Microsoft world, the Mutex is waited on with WaitForSingleObject(), which is described as

If the object's state is nonsignaled, the calling thread enters the wait state until the object is signaled or the time-out interval elapses.

Now you need to know that the "wait state" is a state where the thread is not active. We call it "blocking", which is the opposite of a busy wait where CPU time is used.

At that beginning, the kernel will immediately give the CPU to another thread and never give it back to your thread, unless the Mutex is becoming "signaled". So it will really use 0 CPU cycles during the wait.

When the kernel notices that the Mutex has changed, it can "wake up" the thread and might even boost its priority because it was waiting friendly all the time.

CodePudding user response:

Typically the thread will attempt to acquire the mutex, and if it can't (e.g. because another thread already acquired it) it will inform the scheduler and the scheduler will block the waiting thread and switch to a different thread, and then (later, when the lock is released) the scheduler will unblock the waiting thread and give it CPU time again.

On single-CPU systems; this is almost required. All CPU time spent (e.g. "spinning"/polling the lock again) between finding out the lock can't be acquired and doing a task switch (to a thread that may release the lock) is a waste of CPU time that will achieve nothing (because no other thread can release the lock until a task switch occurs).

However, research on multi-CPU systems (that I vaguely remember from about 20 years ago that may or may not have been done by Sun for Solaris) indicates that a small amount of "spinning" (in the hope that a thread running on a different CPU releases the lock in time) can be beneficial (by avoiding the cost of task switch/es). My intuition is that "time spent spinning before blocking" should be roughly equal to the cost of a task switch (or, if a task switch costs 123 microseconds, it'd probably be worthwhile spinning for 123 microseconds before the scheduler is told to block your thread); but this would depend heavily on scenario (e.g. how heavily contended the lock is, etc).

  • Related