Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This may be a very simple question. I'm curious how blocking calls are implemented. Specifically, how do they block? Is this just thread.sleep?

share|improve this question
on which OS? on what platform? there's probably a ton of variation in low-level details. –  Doug T. Nov 26 '12 at 18:53
@DougT. I'm looking for a general, high-level explanation, if it exists. Specifically, I was implementing a socket class in C# and one of the .Net socket methods blocks until data is received. I was just curious how that works. –  ConditionRacer Nov 26 '12 at 18:54

3 Answers 3

up vote 8 down vote accepted

Specifically, I was implementing a socket class in C# and one of the .Net socket methods blocks until data is received. I was just curious how that works.

There are several ways to implement a blocking call. The obvious way to do it is to return when the work is done See Robert Harvey's Answer.

In the case where there is no work to be done (e.g., waiting for a signal or for input), there are several choices:

  1. Spinlock. Basically, code like while(signal not found){}. Spinlocks have almost no overhead and return faster than other methods, but burn CPU until they return. They are useful if the signal you are waiting for is going to return fast or if giving up a timeslice is to be avoided, but are generally a bad idea in high-level code.
  2. Locks, Mutexes, etc. In C#, these are accessed with things like lock(obj){} (cause other threads to block) and ManualResetEvent (block until another thread signals). Generally, there are implemented at the kernel or hardware level. E.g., C# lock is implemented on x86 machines using the assembly lock cmpxchg assembly instruction (see Junfeng Zhang's blog).
share|improve this answer
Eric Lippert's article, How does locking work in C#? goes into more detail on how lock(obj){} is implemented, but his explanation is, in his own words, "chock full of lies." It's intended more to explain how locks can be implemented rather than how locks are actually implemented (as the latter is more complex). –  Brian Feb 14 '14 at 21:35

Methods don't return control to the caller until their work is completed, that's all. In your socket class example, the method doesn't return the data until it actually receives it.

In most cases, where you're seeing something like Thread.Sleep(1000) in a code example, you are not seeing blocking per se, but merely simulating work.

Where Thread.Sleep is being used legitimately, it is relinquishing control to other threads, with an implicit statement: "I can wait awhile before you return control to me." Thread.Sleep is not required; the operating system will normally preempt the thread for short periods of time anyway, if work on other threads also needs to be completed.

share|improve this answer

In most system calls on most OS's, your blocking call ultimately causes some request to go out to be executed by the kernel while the calling thread is suspended by the scheduler/dispatcher until that work is complete. Typically this work is some kind of IO request to peripheral hardware (ie network card/hard disk/etc). The peripheral hardware can use a processor interrupt to signal the CPU/kernel that some piece of work is complete (or some other piece of info about the work). Once the work is complete, the kernel wakes up the calling thread and allows execution to continue from where it was waiting. The hardware/kernel together have prepared any outputs (buffers/errors etc) for the user-facing calling function and these outputs are handed back to the caller.

The specific details of how this happens are going to be very different depending on the underlying OS. For a deep dive into how Windows works, I recommend: Scheduling, Thread Context, and IRQL.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.