On Thu, 28 Sep 2000, Richard Cook wrote:

> Jeff Newmiller wrote:
> > 
> > On Thu, 28 Sep 2000, Richard Cook wrote:
> > 
> > > Anyone know why pthread_cond_timedwait doesn't seem to wait at all
> > > (noone sends a cond_signal) for the following snippet?  I guess maybe
> > > I'm doing something wrong with  TIA
> > >
> > >   struct timespec theTimeSpec = {0};
> > >   theTimeSpec.tv_sec = 0;
> > >   theTimeSpec.tv_nsec = 500000000;/* 500.0 milliseconds */
> > >   err = pthread_cond_timedwait(&pcbReadyForSorter_cond,
> > > &sortWaiting_lock,
> > >                          &theTimeSpec);
> > >
> > > when it returns, err = 147, which I can't seem to find the meaning of.
> > > When I try to test (err == ETIMEDOUT), my compiler complains that
> > > ETIMEDOUT is not defined.  Wasup?
> > 
> > Well, Reading The Fine Manual it says that the time specification must be
> > absolute, for one thing.  See the example in the man page.
> 
> I don't have an example in my man page.

ah.. sorry..

>  Can you send it to me?  thx.  I
> still don't understand what "absolute" means.  All time is relative. 
> (and I'm not just being philosophical here)

Relative is relative to when you ask for the timeout to occur.
Absolute is relative to an epoch... Unix time zero in this case,
1 January 1970.  I think from the example you can see why these semantics
are used.

Quote:
------
Consider two shared variables x and y,  protected  by  the
mutex  mut,  and  a  condition variable cond that is to be
signaled whenever x becomes greater than y.
  
  int x,y;
  pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 
[...]

To  wait for x to becomes greater than y with a timeout of
5 seconds, do:

  struct timeval now;
  struct timespec timeout;
  int retcode;

  pthread_mutex_lock(&mut);
  gettimeofday(&now);
  timeout.tv_sec = now.tv_sec + 5;
  timeout.tv_nsec = now.tv_usec * 1000;
  retcode = 0;
  while (x <= y && retcode != ETIMEDOUT) {
    retcode = pthread_cond_timedwait(&cond, &mut, &timeout);
  }
  if (retcode == ETIMEDOUT) {
    /* timeout occurred */
  } else {
    /* operate on x and y */
  }
  pthread_mutex_unlock(&mut);
------

> > As for ETIMEDOUT, the only instance of this macro to be found in my
> > include tree is in /usr/include/pi-source.h, which is for Pilot
> > programming.  I think this is a deficiency in the pthreads library.
> 
> I found a definition in /usr/include/sys/errno.h

Definitely not in mine.  Bizarre... my man page is more complete than
yours, but your headers are more up-to-date than mine.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[EMAIL PROTECTED]>        Basics: ##.#.       ##.#.  Live Go...
Work:<[EMAIL PROTECTED]>              Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...2k
---------------------------------------------------------------------------

Reply via email to