简体   繁体   中英

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.
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.
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. In case of Linux look at respective man pages. For platform independent solution you can look at libevent library.

... takes at the max 3microsec and minimum 600nanosec.

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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