This is an exam question (practice exam, not the real one). It's about concurrent programming using a multi-core processor and the problems with using locks.

In concurrent programming is it possible that, by using locks, a program might sometimes use more processors than are necessary

In other words, is this ever possible? It's a true/false question. I can't find an answer anywhere and I'm revising for my exams.

2

There are 2 best solutions below

0
On BEST ANSWER

The concurrent program with N threads of execution using locks at any point in time can have M=0 .. N-1 threads waiting for locks; thus this program can only be utilizing N-M processors since waiting for a lock does not require a processor. Thus, no, using locks does not increase the number of processors required by a concurrent program.

0
On

With an efficient implementation of multi-threading and locks, if a thread blocks waiting for a lock for any significant time, the scheduler / lock implementation will reassign the core to do something else.

But since the exam question is asking if it is ever possible to use more processors than are strictly necessary, the answer is that it depends on the implementation of threads / locks / scheduling. For instance, there is a kind of lock called a spinlock where the lock implementation does NOT surrender control of the processor while waiting to acquire a lock. Instead, it polls the lock in a tight loop trying to acquire it.

Why would you do that? Well, if the lock is likely to become available in a short enough period of time, then the CPU wasted "spinning" on the lock is less than would be spent on performing a full context switch.

So I don't think your exam question has a simple yes / no answer.