简体   繁体   English

在线程中调用boost :: asio :: read()会阻止调用线程或进程?

[英]Calling boost::asio::read() in a thread blocks calling thread or process?

I'm quite new to network programming and I'm writing a program that should accept many TCP connections and receive data from them. 我是网络编程的新手,我正在编写一个程序,该程序应接受许多TCP连接并从中接收数据。 To make things go parallel, the agent should read data from each socket in a new thread. 为了使事情并行进行,代理应从新线程中的每个套接字读取数据。 I decided to use boost::asio instead of raw *nix sockets to make things simpler. 我决定使用boost::asio而不是原始的* nix套接字来简化事情。 Though this seems to be a wrong decision... 虽然这似乎是一个错误的决定...

I wonder if I calling boost::asio::read or boost::asio::read_some blocks only its calling thread or blocks process? 我想知道我是否调用boost::asio::readboost::asio::read_some仅阻止其调用线程或阻止进程? Yes I should write my own small test and see results myself, but I have no access to my Linux box right now. 是的,我应该编写自己的小型测试并亲自查看结果,但是我现在无法访问Linux。 Just thinking about code that I should write tomorrow at university. 只考虑我明天应该在大学写的代码。

So if it blocks the process, what's correct way of implementing a server/client architecture that accepts many clients at same time? 因此,如果阻止了该过程,那么实现同时接受多个客户端的服务器/客户端架构的正确方法是什么?

Notes: 笔记:

  1. I'm having difficulties about design decisions. 我在设计决策方面遇到困难。 Any suggestion is appropriate. 任何建议都是适当的。

The read and read_some calls are both blocking, and will only block the current thread for Linux and Win32 (and probably most others, just don't have direct expericence). read和read_some调用均处于阻塞状态,并且只会阻塞Linux和Win32的当前线程(可能还有其他大多数线程,只是没有直接经验)。

You might want to look into using async_read instead though if you are having a large number of incoming connections, as you might acctually do better performance wise using a smaller number of threads than number of connections. 如果您有大量的传入连接,则可能要改用async_read,因为使用数量少于连接数的线程可能会准确地提高性能。 Boost does provide examples of using the thread pool to handle client connections. Boost确实提供了使用线程池处理客户端连接的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM