This is the mail archive of the mailing list for the pthreas-win32 project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: pthread_cond_timedwait

tests\stress1.c uses the following routine. The args are: a pointer to a
timespec instance and; the number of millisecs into the future. It
returns the first arg so you can call it directly from within
pthread_cond_wait's argument list:

struct timespec *
millisecondsFromNow (struct timespec * time, int millisecs)
  struct _timeb currSysTime;
  int64_t nanosecs, secs;
  const int64_t NANOSEC_PER_MILLISEC = 1000000;
  const int64_t NANOSEC_PER_SEC = 1000000000;

  /* get current system time and add millisecs */

  nanosecs = ((int64_t) (millisecs + currSysTime.millitm)) * NANOSEC_PER_MILLISEC;
  if (nanosecs >= NANOSEC_PER_SEC)
      secs = currSysTime.time + 1;
      nanosecs %= NANOSEC_PER_SEC;
      secs = currSysTime.time;

  time->tv_nsec = (long)nanosecs;
  time->tv_sec = (long)secs;

  return time;


On Fri, 2005-06-10 at 21:23 +0100, Steve Croall (TIBCO) wrote: 
> Hi,
> The timeout parameter you pass to pthread_cond_timedwait() is a struct 
> timespec type, which is defined in pthread.h, if HAVE_STRUCT_TIMESPEC is 
> not defined at build time.
> struct timespec {
>          long tv_sec;
>          long tv_nsec;
> };
> Just set the tv_nsec to the amount of nanoseconds required.  The pthread 
> source on Windows uses the following calculation to convert the above 
> structure to milliseconds:
>    tmpAbsMilliseconds =  (int64_t)abstime->tv_sec * MILLISEC_PER_SEC;
>    tmpAbsMilliseconds += ((int64_t)abstime->tv_nsec + 
> Using the following defines:
>    const int64_t NANOSEC_PER_MILLISEC = 1000000;
>    const int64_t MILLISEC_PER_SEC = 1000;
> Steve.
> Allan Comar wrote:
> > Hi all, I am needing something that I couldn't find in nowhere else, I am trying to use pthread_cond_timedwait but I want that the time that I need to wait is setted im milliseconds, I already could do in seconds and i am having a real hard time trying it in milliseconds. Any Ideas ? I am using MSVC6.0 with pthreads.
> > 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]