Yanshof
Yanshof

Reputation: 9926

Is 'Lock' taking CPU time?

I have 6 threads. One of the thread get in some scope and turn on the 'lock' and all the other threads are waiting and want to enter to the same scope.

Now, Is the other threads will get CPU Time? Does the other thread are in the thread schedule ? I understand that all the other thread are in waiting state - but the CPU will try to make the thread continue and try to get in the scope ( even if the scope is not accessible )

Upvotes: 11

Views: 2736

Answers (2)

Asken
Asken

Reputation: 8071

That's not how lock works. You won't give anyone more "cpu time". If you're locking you might introduce wait periods and potentially deadlocks where the threads are waiting for each other.

Upvotes: 0

CodesInChaos
CodesInChaos

Reputation: 108830

When trying to enter a lock that's already taken, threads first spinlock for a while, and finally suspend and enter a wait state.

They still burn CPU time while they spin, but no longer once they wait.

Upvotes: 14

Related Questions