So imagine I have code similar to the following:
var obj = new object();
lock (obj)
{
while (true);
}
Since the loop will run forever, and since the loop is in a critical section:
Will this take up all of the processor's time, never yielding to another thread? I believe once a processor is in a critical section it cannot be interrupted until it leaves the critical section, so that the operations in the lock are atomic.
Could this technique be used to guarantee that your application always has a core to run on, and is never waiting?
No, at the operating system level, the thread will be suspended and another thread (from another process or not) will be executed instead. The operating system scheduler is in charge of assigning CPU time to the various threads executing in the system, depending on their state and priority.
See MSDN (especially multitasking and scheduling chapters).
Also,
lock
doesn't meanatomic
. It just means no other thread can run the same piece of code at the same time.