1

I have a thread whose job is to send messages to UDP peers. the threads sends the messages iff one of the following apply:

1) a certain time has passed since the last time it sent a message (like a timeout).

2) an update boolean flag in a shared struct has been raised by other thread.

i want to be able to wait for these conditions to happen so i would know when to send the message.

the simplest way i can do it is by making a loop that repeats until one of the conditions satisfy. i'm afraid it is busy waiting and will consume a lot of CPU time for nothing. I don;t want to use sleep() either.

i don't mind for a C++ solution as long as it's easy to understand and implement since i'm not very familiar with C++.

Thanks !

Michael
  • 22,196
  • 33
  • 132
  • 187

4 Answers4

11

For windows, use an event (CreateEvent) rather than a bool, then WaitForSingleObject on it.

Erik
  • 88,732
  • 13
  • 198
  • 189
  • that takes care of the 2nd condition, i'll replace it with an event. for the first condition, how do i wait for time to pass in windows ? – Michael Apr 19 '11 at 12:56
  • 1
    @michael: With WaitForSingleObject - pass a number of milliseconds to it. It'll return when *either* the event is signaled, or the wait has timed out. – Erik Apr 19 '11 at 12:57
2

For the signal between threads I agree with Erik. Use an event object.

For the timeout problem you can use CreateWaitableTimer() and SetWaitableTimer().

To wait in the thread for the event to get signaled or the timer to run out you can use the wait function WaitForMultipleObjects(). You can pass an array of handles (= handle to event and handle to timer) to it to wait for them getting signaled.

The wait functions have the advantage of not using up the CPU, like a polling loop would do since the waiting is handled on the kernel level and the thread gets suspended while it is waiting for an object to get signaled.

Dolphin
  • 299
  • 1
  • 2
0

You most likely want to use a callback routine embedded in the thread. See What is a “callback” in C and how are they implemented? for an explanation and check out the C example in this wiki example which implements a callback as a function pointer. It's easier to use an event framework of some type (as suggested by Erik et. al.) rather than roll your own, but it's a handy skill to have in your bag of tricks.

Community
  • 1
  • 1
Marc
  • 4,546
  • 2
  • 29
  • 45
  • 1
    Hangon.... he needs to wait for a signal *or* a timeout, there's an API call doing *exactly* that, but he should do it with a more complex mechanism? – Erik Apr 19 '11 at 13:04
  • @Erik - +1 for you: As I said, ... it's easier to use an event framework of some type (as suggested by Erik et. al.) rather than roll your own, but it's a handy skill to have in your bag of tricks. Having had to do it once before (in C by the way) was very helpful to me in getting a better understanding of how event handling is done in the real world. Just pointing out a "teachable moment"... – Marc Apr 19 '11 at 13:10
0

I would use WaitForMultipleObjects, listening for 2 events The first would be to exit the thread and the second to do the work.

Something like this...

DWROD dwTimeOut = .....;


DWORD dwRetValue = WaitForMultipleObjects(2, hEvents, false, dwTimeOut);

if( (dwRetValue ==  WAIT_TIMEOUT) || (dwRetValue == (WAIT_OBJECT_0 + 1)))
{
  // Do Work

}
else if(dwRetValue == WAIT_OBJECT_0)
{
  // End Thread

}
else
{
  // Deal with Error
}
João Augusto
  • 2,285
  • 24
  • 28