简体   繁体   English

无阻塞发送功能

[英]Non-Blocking sendto function

I am using blocking sendto (flag set to 0) function in my cpp code which takes at the max 3microsec and minimum 600nanosec. 我在我的cpp代码中使用了阻塞sendto(标志设置为0)功能,该功能的最大接收时间为3微秒,最小接收时间为600nanosec。
I want a method that is non-blocking(ie returns immediately) and takes less time. 我想要一个非阻塞的方法(即立即返回)并且花费更少的时间。
I tried using sendto with flag set to MSG_DONTWAIT, it was found that the non-blocking sendto is similar to blocking sendto in terms of latency. 我尝试使用将标志设置为MSG_DONTWAIT的sendto,发现非阻塞sendto在延迟方面类似于阻塞sendto。
Please suggest an alternative method which is non-blocking and time-efficient. 请提出一种非阻塞且省时的替代方法。

You need to use select () or epoll() alike technique to find out when exactly the socket becomes writable. 您需要使用select ()epoll()类似的技术来找出套接字何时确切可写。 In case of Linux look at respective man pages. 如果是Linux,请查看相应的man页。 For platform independent solution you can look at libevent library. 对于独立于平台的解决方案,您可以查看libevent库。

... takes at the max 3microsec and minimum 600nanosec. ...最多需要3微秒,最少600纳秒。

This is the time which is needed by the system to put the message into the socket buffer which involves a system call. 这是系统将消息放入涉及系统调用的套接字缓冲区所需的时间。 This does not include the sending to the peer itself which is done later in the kernel. 这不包括发送到对等点本身,而稍后在内核中完成。 This also means that it does not matter if you use a blocking or a non-blocking sendto because putting the message into the socket buffer need to be done in both cases. 这也意味着使用阻塞还是非阻塞sendto都没有关系,因为在两种情况下都需要将消息放入套接字缓冲区。 This also means that no select , epoll , boost::asio or whatever will help to make this faster since these don't reduce the time needed to put the message into the socket buffer. 这也意味着没有selectepollboost::asio或其他任何方法都可以使此操作更快,因为它们不会减少将消息放入套接字缓冲区所需的时间。

The only difference between blocking and non-blocking sendto is that the first will wait for the system to make room in the socket buffer in case the buffer was already full, ie if you send messages faster than the system can deliver the messages. 阻塞发送和非阻塞发送之间的唯一区别是,第一个将等待系统在套接字缓冲区中腾出空间,以防缓冲区已满,即,如果您发送消息的速度比系统传递消息的速度快。

It is unknown what your application really does but a way to speed it up might be to reduce the number of sendto calls by using larger messages. 尚不知道您的应用程序实际执行的操作,但加快速度的一种方法可能是通过使用较大的消息来减少sendto调用的次数。

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

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