0

I am implementing a multi cast server that sends a message every X amount of seconds to a multicast address.

I am also part of the multicast group and I will also receive messages from other senders in that group.

My question is, can I use sleep(X) to send my message while still receiving other messages from the group and process them? Or does sleep() block?

umdcoder
  • 137
  • 1
  • 2
  • 15

3 Answers3

3

Sleep blocks all execution, but only in the thread from which you call it. I would suggest that you create two threads, one for broadcasting and one for listening. Then make sure that you synchronize any data shared between the threads with Mutexes.

spectacularbob
  • 3,080
  • 2
  • 20
  • 41
2

When you call sleep(), only the calling thread gets suspended. All other threads will continue running, so you can continue receiving the data on the concurrently running threads.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Yes, sleep is blocking. You haven't said how you're implementing the server, but if it's in terms of a select loop, you should use the timeout argument to select, together with gettimeofday or clock_gettime and some arithmetic to determine when the next time you should send a message is, whether you've passed that time, and if not, how long until the time is up (which you can use for the select timeout). The timeradd, timersub, and timercmp macros can help with that.

hobbs
  • 223,387
  • 19
  • 210
  • 288
  • I am actually planning on using poll() for this – umdcoder Nov 11 '14 at 03:58
  • @umdcoder same applies, except that `poll` takes its timeout as an integer number of milliseconds instead of a `struct timeval` so you have to convert a bit. – hobbs Nov 11 '14 at 04:14