前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >What's the Difference Between Blocking vs Non-Blocking and Sync vs Async?

What's the Difference Between Blocking vs Non-Blocking and Sync vs Async?

作者头像
ppxai
发布2023-11-18 08:38:44
1300
发布2023-11-18 08:38:44
举报
文章被收录于专栏:皮皮星球皮皮星球

What's the Difference Between Blocking vs Non-Blocking and Sync vs Async?

Overview

During application development, we often confront terms like "Blocking," "Non-blocking," "Synchronous," and "Asynchronous." It's a common misconception to view these as synonymous. In reality, they represent distinct, albeit intertwined, concepts.

One frequent point of confusion is between "Blocking" and "Synchronous" and between "Non-blocking" and "Asynchronous."

In essence:

  • "Synchronous" and "Asynchronous" processes involve at least two subjects. When their task durations align, it's "Synchronous"; if not, it's "Asynchronous."
  • "Blocking" and "Non-blocking" describe how systems handle tasks. These terminologies are primarily applied to I/O operations.

Let's delve into these concepts in more detail.

Concept

1. Understanding Blocking vs. Non-Blocking

The crux of this distinction lies in how a system responds when waiting for another process.

  • Blocking: The system waits for another process to complete before resuming its task. Think of a Java JDBC querying a database—it waits for a response.
  • Non-Blocking: The system continues its operation, irrespective of other processes. It doesn't wait.

2. Deciphering Synchronous vs. Asynchronous

When considering multiple subjects executing tasks:

  • Synchronous: The tasks align, either starting or ending simultaneously.
  • Asynchronous: Task start/end times are independent of each other.

3. Blocking/Non-blocking vs. Sync/Async

When an I/O function is invoked:

  • Blocking: Waits for the process to complete before returning.
  • Non-Blocking: Returns immediately, regardless of whether the task has finished.

In terms of who oversees task completion for the I/O function:

  • Synchronous: Managed by the application.
  • Asynchronous: Overseen by the kernel.

Based on the combinations of Blocking/Non-Blocking and Synchronous/Asynchronous, the four resulting quadrants are shown in the diagram below:

Examples of Their Combinations

  1. Synchronous Blocking I/O

This is the most straightforward method of I/O. When you make a call, the program waits for the operation to complete before moving on.

代码语言:javascript
复制
#include <iostream>
#include <fstream>

int main() {
    std::ifstream file("example.txt");
    std::string content;

    // This line blocks until the entire file is read
    std::getline(file, content, '\0');

    std::cout << content;

    file.close();
    return 0;
}
  1. Synchronous Non-blocking I/O

In this mode, the system would regularly check (or poll) if data is available. If not, it would continue doing something else.

代码语言:javascript
复制
// This is a simplified example. True non-blocking I/O in C++ can be complex.
// Assuming a non-blocking I/O library function 'try_read'

/*
bool try_read(std::ifstream& file, std::string& content);
*/

int main() {
    std::ifstream file("example.txt");
    std::string content;

    while (!try_read(file, content)) {
        // Do other tasks or sleep for a while
    }

    std::cout << content;

    file.close();
    return 0;
}
  1. Asynchronous Blocking I/O

This sounds contradictory, but it can be thought of as starting an I/O operation asynchronously, but once started, that particular I/O operation would block until completion. This is often used with I/O multiplexing.

代码语言:javascript
复制
// An example using pseudo-code, as actual implementations vary.
// Assuming a function 'async_read' that starts reading and blocks until it completes.

/*
void async_read(std::ifstream& file, void (*callback)(std::string content));
*/

void print_content(std::string content) {
    std::cout << content;
}

int main() {
    std::ifstream file("example.txt");

    async_read(file, print_content);

    // Do other tasks while I/O is ongoing

    file.close();
    return 0;
}
  1. Asynchronous Non-Blocking I/O (AIO):

In this mode, the I/O operation starts and the control returns immediately, allowing the program to continue executing subsequent statements without waiting.

代码语言:javascript
复制
// Assuming a library function 'async_read_nonblocking'
/*
void async_read_nonblocking(std::ifstream& file, void (*callback)(std::string content));
*/

void print_content(std::string content) {
    std::cout << content;
}

int main() {
    std::ifstream file("example.txt");

    async_read_nonblocking(file, print_content);

    // Do other tasks immediately without waiting for the I/O to complete

    // Wait or poll for the asynchronous I/O to complete if needed

    file.close();
    return 0;
}

Conclusion

These concepts revolve around how applications and kernels interact. Distinguishing between them requires understanding the difference between the application and kernel domains. Reviewing the explanations, associated diagrams, and code examples above can provide a clearer picture.

References

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-07-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • What's the Difference Between Blocking vs Non-Blocking and Sync vs Async?
    • Overview
      • Concept
        • 1. Understanding Blocking vs. Non-Blocking
        • 2. Deciphering Synchronous vs. Asynchronous
        • 3. Blocking/Non-blocking vs. Sync/Async
      • Examples of Their Combinations
        • Conclusion
          • References
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档