Is it better to use sleep () or an infinite loop to wait for an event?

I am currently working on a client that is talking to an SSH server. Everything works fine, but since the server is responding rather slowly, I have to wait for it to send data. I have two options, and I would like to get your advice on what is the most efficient way to wait for the server.

Choice number 1:

while (!(ssh_channel_poll(*sshChannel,0)))
 ;

Choice number 2:

while (!(ssh_channel_poll(*sshChannel,0)))
  sleep(1);
+4
source share
4 answers

Both alternatives are undesirable. Usually you should use read lock. I assume it looks something like this (as you say you are expecting a server):

while (!ssh_channel_poll(...)) { ... }
ssh_channel_read(...);

poll . , SSH , read , , , .

// This is all you need.
ssh_channel_read(...);
+8

sleep , , , . sleep, CPU .

+1

- . (...) , (...).

0

Source: https://habr.com/ru/post/1684590/


All Articles