how to improve the efficiency?

the structure of my socket programme is below:
the purpose of it is “send” and “receive” data by special period,I found the occupancy of CPU is very HIGH when I use sleep function,Can I change it to improve the efficiency?how to do

More info is needed to provide an alternate solution. If the socket is not set to non blocking, this should not take a lot of CPU, unless the functions are returning error code and you aren’t checking for them.

The usleep(10) may not do what you thing it’s doing. usleep cannot be more precise then the system clock period which is set a 1ms by defaults. A request of 10us will give you in fact 1 ms maybe 2ms.

yes,I have set the socket to non blocking,because I don’t want it blocked when it can’t receice or send anything

Thinking about it, still should not take that much CPU. Unless there is something in your real code that doesn’t show in your pseudo code. A good rule of thumb is never do polling ;-)

For tcpip stuff I like to go with a thread and leave the socket in block mode, that way I don’t have to use select/ionotify which in my opinion adds a significant overhead compare to a simple recv loop