Chouchenn Chouchenn - 22 days ago 175
C Question

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

I'm currently working on a client that talk with a SSH server.
Everything works well, but, because the server is quite slow to answer, I have to wait for it to send data.
I have two choices, and I'd like to have your advice about what is the most efficient way to wait for the server.

Choice #1 :

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


Choice #2 :

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

Answer Source

Both alternatives are undesirable. Normally, you would use a blocking read. I assume it looks something like this (since you say you are waiting for the server):

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

In this case, the poll is unnecessary. Just make sure the SSH connection is a blocking connection, and the read function will wait until data is available if none is available when you call it.

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