Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

sleep

libssh

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);
like image 921
Chouchenn Avatar asked Dec 19 '22 04:12

Chouchenn


1 Answers

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(...);
like image 177
Dietrich Epp Avatar answered Dec 28 '22 11:12

Dietrich Epp