liveBook · Manning

Blocking

As explained in C# Concurrency: Asynchronous and multithreaded programming

A blocking operation is an operation that prevents its thread from making progress while waiting for external data, another thread, or another condition.

A blocking operation makes its thread wait for external data, another thread, or another condition before continuing. File access, network access, and other communication outside the CPU and memory can block; so can operations that wait for another thread. The operating system removes a blocked thread from CPU scheduling until the wait ends. section 1.1

Blocking is mainly a resource problem rather than a CPU problem. Each blocked thread still consumes memory, and managing many threads adds context-switching overhead. A program that creates too many blocked threads can become slower, exhaust memory, or crash. section 1.1 section 1.6

Prefer nonblocking asynchronous operations when work mostly waits:

  • Use an asynchronous API so the thread is released while external work proceeds and resumes after completion. section 1.6
  • Avoid converting asynchronous work back into blocking with Task.Wait() or Task.Result; unsupported usage can cause bugs, deadlocks, or exceptions and removes the asynchronous benefit. section 6.3.1
  • Do not overload the thread pool with long-running blocking work; blocked pool threads cannot process other work efficiently. section 8.1.3

Camera capture

A synchronous camera method acquires the camera, performs a blocking capture, and releases the camera. The asynchronous version awaits TakePictureAsync before releasing it, preserving the required order without occupying the thread during the capture. section 6.3.1

Figure 1.2 Single-threaded versus multithread with multiple requests

Compare single-threaded and multithreaded handling of multiple waiting requests. section 1.1

How C# Concurrency: Asynchronous and multithreaded programming covers it

How deep it goes
Defines operating-system blocking, thread costs, and thread-pool effects, then stops short of low-level implementation details.
Examples
Uses file and network access, camera capture, mail sending, Task.Wait, Task.Result, Task.Delay, and thread-pool benchmarks.
What it assumes
Assumes familiarity with threads and methods while introducing blocked states, asynchronous operations, and thread-pool consequences.
What you can do after
Diagnose blocked threads and choose asynchronous APIs when workloads spend most of their time waiting.
How it connects
Connects blocking operations to threads, context switching, asynchronous programming, thread pools, deadlocks, and cancellation-independent waiting.

Also explained in

Compare how 9 books explain blocking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C# Concurrency: Asynchronous and multithreaded programming. The section links point to the parts of the book it draws on.