I wrote that nice capability to listen to UDP messages and these are added to a FIFO whenever a new message arrives and that is signaled to a listener.
The listener waits for messages if it has nothing else to do. However, it knows, in some cases, that it should wake up in a very short amount of time. So I wrote code to use the pthread_cond_timedwait() and there I put the time which in my current test 1.5 seconds, roughly.
It does wait 1 second, then the wait function does not block anymore. Does that mean the current implementation does not support sub-seconds (milli/micro second waits)?
There is a little bit of my output. I start with 1417 milli-seconds, and it seems to wait for 1001 milli-seconds on the first attempt. Then 0 or 1 millisecond on each following attempt, non-blocking at all.
image transform ending with [1416272]
wait for 1417 till 1401354361 now = 1401354360
image transform ending with [415259]
wait for 416 till 1401354361 now = 1401354361
image transform ending with [414759]
wait for 415 till 1401354361 now = 1401354361
image transform ending with [414196]
wait for 415 till 1401354361 now = 1401354361
image transform ending with [413646]
wait for 414 till 1401354361 now = 1401354361
image transform ending with [413013]
wait for 414 till 1401354361 now = 1401354361
image transform ending with [412385]
wait for 413 till 1401354361 now = 1401354361
image transform ending with [411801]
wait for 412 till 1401354361 now = 1401354361
image transform ending with [411237]
wait for 412 till 1401354361 now = 1401354361
image transform ending with [410690]
wait for 411 till 1401354361 now = 1401354361
image transform ending with [410204]
wait for 411 till 1401354361 now = 1401354361
image transform ending with [409728]
wait for 410 till 1401354361 now = 1401354361
image transform ending with [409150]
wait for 410 till 1401354361 now = 1401354361
image transform ending with [408566]
wait for 409 till 1401354361 now = 1401354361
image transform ending with [408004]
...snip...
wait for 3 till 1401354361 now = 1401354361
image transform ending with [2188]
wait for 3 till 1401354361 now = 1401354361
image transform ending with [1628]
wait for 2 till 1401354361 now = 1401354361
image transform ending with [1077]
wait for 2 till 1401354361 now = 1401354361
image transform ending with [221]
wait for 1 till 1401354361 now = 1401354361
The wait function:
void wait(int msecs)
{
if(msecs == -1)
{
pthread_cond_wait(&f_condition, &f_mutex.f_mutex);
}
else if(msecs > 0)
{
struct timeval tod;
gettimeofday(&tod, nullptr);
struct timespec ts;
ts.tv_sec = tod.tv_sec + msecs / 1000;
ts.tv_nsec = tod.tv_usec * 1000 + msecs % 1000;
ts.tv_sec += ts.tv_nsec / 1000000000L;
ts.tv_nsec = ts.tv_nsec % 1000000000L;
std::cerr << "wait for " << msecs << " till " << ts.tv_sec << " now = " << time(NULL) << "\n";
pthread_cond_timedwait(&f_condition, &f_mutex.f_mutex, &ts);
}
}