public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
* pthread_cond_timedwait
@ 2005-06-10 18:07 Allan Comar
  2005-06-10 20:23 ` pthread_cond_timedwait Steve Croall (TIBCO)
  0 siblings, 1 reply; 7+ messages in thread
From: Allan Comar @ 2005-06-10 18:07 UTC (permalink / raw)
  To: pthreads-win32


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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: pthread_cond_timedwait
  2005-06-10 18:07 pthread_cond_timedwait Allan Comar
@ 2005-06-10 20:23 ` Steve Croall (TIBCO)
  2005-06-11  0:56   ` pthread_cond_timedwait Ross Johnson
  0 siblings, 1 reply; 7+ messages in thread
From: Steve Croall (TIBCO) @ 2005-06-10 20:23 UTC (permalink / raw)
  To: Allan Comar; +Cc: pthreads-win32

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 + 
(NANOSEC_PER_MILLISEC/2)) / NANOSEC_PER_MILLISEC;

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.
> 

-- 

J. Senior Software Engineer, Tibco Software Ltd.
T. +44 (0) 1792 360773
M. +44 (0) 7788 971394
E. scroall@tibco.com
W. www.tibco.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: pthread_cond_timedwait
  2005-06-10 20:23 ` pthread_cond_timedwait Steve Croall (TIBCO)
@ 2005-06-11  0:56   ` Ross Johnson
  0 siblings, 0 replies; 7+ messages in thread
From: Ross Johnson @ 2005-06-11  0:56 UTC (permalink / raw)
  To: Allan Comar; +Cc: Pthreads-Win32 list

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 */
  _ftime(&currSysTime);

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

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

  return time;
}

Ross

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 + 
> (NANOSEC_PER_MILLISEC/2)) / NANOSEC_PER_MILLISEC;
> 
> 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.
> > 
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: pthread_cond_timedwait
@ 1999-09-09 10:51 Bossom, John
  0 siblings, 0 replies; 7+ messages in thread
From: Bossom, John @ 1999-09-09 10:51 UTC (permalink / raw)
  To: 'Mikael.Ambrus@elema.siemens.se', pthreads-win32

I just saw a potential bug in your code, below:

assert( pthread_mutex_lock(&(rdwrp->mutex)) == 0 );

I recommend that you do not call functions within an 'assert'
unless you don't mind the compiler optimizing out the 'assert'
statement! (i.e. your lock will not happen)

Quote from MS Dev Studio, 5.0 for 'assert':
"The ANSI assert macro is typically used to identify logic errors during
program development, by implementing the expression argument to evaluate to
false only when the program is operating incorrectly. After debugging is
complete, assertion checking can be turned off without modifying the source
file by defining the identifier NDEBUG. NDEBUG can be defined with a /D
command-line option or with a #define directive. If NDEBUG is defined with
#define, the directive must appear before ASSERT.H is included."



I recommend you using:

	int	lockResult;

	lockResult = pthread_mutex_lock(&(rdwrp->mutex));
	assert( lockResult == 0 );


-----Original Message-----
From: Mikael.Ambrus@elema.siemens.se
[ mailto:Mikael.Ambrus@elema.siemens.se ]
Sent: Thursday, September 09, 1999 11:47 AM
To: pthreads-win32@sourceware.cygnus.com
Subject: pthread_cond_timedwait


	[Ambrus Mikael]  Dear pthreads colleagues, 

	I'm writing a program that uses pthread_cond_timedwait. In the book
that I'm using (Pthreads Programming by Nicols, Buttlar & Proulux Farell )
it says that this function should suspend the thread until some other thread
calls  pthread_cond_signal, pthread_cond_broadcast OR the system timer is
greater than or equal to the third argument (abstime). 

	Since clock_gettime is not implemented and I cant find some other
function that tells me the system time, I've tried to implement my own
version of clock_gettime by using the ansi function clock(). This function
returns the number of ticks that has elapsed since the program was started.

	But it seams that this is not the same absolute time that
pthread_cond_timedwait requires since the thread won't awaken.

	Is there another way to acquire the relevant system time?

	Another thing that has bothered me is that I recently downloaded the
latest snip of phtreads and now my read/write locks won't work. Since the
precompiled lib isn't recognised by the linker (neither with gcc v20.1 nor
MSVC 6.0) for some reason, I built the libs using the buildlib.bat provided.
Here's a snip of the code that fails:

	int pthread_rdwr_wunlock_np (
	   pthread_rdwr_t *rdwrp
	){
	   assert( pthread_mutex_lock(&(rdwrp->mutex)) == 0 );
	   if (rdwrp->writers_writing == 0) {
	      assert( pthread_mutex_unlock(&(rdwrp->mutex)) == 0 );
	      return(-1);            
	   }else{
	      rdwrp->writers_writing = 0;
	      assert(pthread_cond_broadcast(&(rdwrp->lock_free)) == 0);

	   };

	   assert( pthread_mutex_unlock(&(rdwrp->mutex)) == 0 );
	   return(0);
	};

	pthread_cond_broadcast returns EINVAL. Does anyone recognise this
problem?

	Wkr
	Michael Ambrus
	Siemens Elema
>  

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: pthread_cond_timedwait
@ 1999-09-09  9:39 Medina, Aurelio
  0 siblings, 0 replies; 7+ messages in thread
From: Medina, Aurelio @ 1999-09-09  9:39 UTC (permalink / raw)
  To: 'Mikael.Ambrus@elema.siemens.se', pthreads-win32

To all,

Speaking of read/write locks, does anyone know if the POSIX read/write lock
routines (e.g. pthread_rwlock_init()) that I submitted will eventually
become part of the Pthreads-Win32 library?  Seems like someone is already
working on another implementation.  The routines that I submitted were
introduced in UNIX98 and are currently a X/Open standard.  These routines
are portable and are currently available on HP-UX 11.0.

Thanks,
Aurelio Medina

	-----Original Message-----
	From:	Mikael.Ambrus@elema.siemens.se
[SMTP:Mikael.Ambrus@elema.siemens.se]
	Sent:	Thursday, September 09, 1999 10:47 AM
	To:	pthreads-win32@sourceware.cygnus.com
	Subject:	pthread_cond_timedwait

		[Ambrus Mikael]  Dear pthreads colleagues, 

		I'm writing a program that uses pthread_cond_timedwait. In
the book
	that I'm using (Pthreads Programming by Nicols, Buttlar & Proulux
Farell )
	it says that this function should suspend the thread until some
other thread
	calls  pthread_cond_signal, pthread_cond_broadcast OR the system
timer is
	greater than or equal to the third argument (abstime). 

		Since clock_gettime is not implemented and I cant find some
other
	function that tells me the system time, I've tried to implement my
own
	version of clock_gettime by using the ansi function clock(). This
function
	returns the number of ticks that has elapsed since the program was
started.

		But it seams that this is not the same absolute time that
	pthread_cond_timedwait requires since the thread won't awaken.

		Is there another way to acquire the relevant system time?

		Another thing that has bothered me is that I recently
downloaded the
	latest snip of phtreads and now my read/write locks won't work.
Since the
	precompiled lib isn't recognised by the linker (neither with gcc
v20.1 nor
	MSVC 6.0) for some reason, I built the libs using the buildlib.bat
provided.
	Here's a snip of the code that fails:

		int pthread_rdwr_wunlock_np (
		   pthread_rdwr_t *rdwrp
		){
		   assert( pthread_mutex_lock(&(rdwrp->mutex)) == 0 );
		   if (rdwrp->writers_writing == 0) {
		      assert( pthread_mutex_unlock(&(rdwrp->mutex)) == 0 );
		      return(-1);            
		   }else{
		      rdwrp->writers_writing = 0;
		      assert(pthread_cond_broadcast(&(rdwrp->lock_free)) ==
0);

		   };

		   assert( pthread_mutex_unlock(&(rdwrp->mutex)) == 0 );
		   return(0);
		};

		pthread_cond_broadcast returns EINVAL. Does anyone recognise
this
	problem?

		Wkr
		Michael Ambrus
		Siemens Elema
	>  

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: pthread_cond_timedwait
  1999-09-09  8:51 pthread_cond_timedwait Mikael.Ambrus
@ 1999-09-09  9:21 ` Scott Lightner
  0 siblings, 0 replies; 7+ messages in thread
From: Scott Lightner @ 1999-09-09  9:21 UTC (permalink / raw)
  To: pthreads-win32

_ftime() will give you the current time with milliseconds.

I believe struct timespec is in nanoseconds so you will lose some precision.

Scott
----- Original Message -----
From: <Mikael.Ambrus@elema.siemens.se>
To: <pthreads-win32@sourceware.cygnus.com>
Sent: Thursday, September 09, 1999 11:47 AM
Subject: pthread_cond_timedwait


> [Ambrus Mikael]  Dear pthreads colleagues,
>
> I'm writing a program that uses pthread_cond_timedwait. In the book
> that I'm using (Pthreads Programming by Nicols, Buttlar & Proulux Farell )
> it says that this function should suspend the thread until some other
thread
> calls  pthread_cond_signal, pthread_cond_broadcast OR the system timer is
> greater than or equal to the third argument (abstime).
>
> Since clock_gettime is not implemented and I cant find some other
> function that tells me the system time, I've tried to implement my own
> version of clock_gettime by using the ansi function clock(). This function
> returns the number of ticks that has elapsed since the program was
started.
>
> But it seams that this is not the same absolute time that
> pthread_cond_timedwait requires since the thread won't awaken.
>
> Is there another way to acquire the relevant system time?
>
> Another thing that has bothered me is that I recently downloaded the
> latest snip of phtreads and now my read/write locks won't work. Since the
> precompiled lib isn't recognised by the linker (neither with gcc v20.1 nor
> MSVC 6.0) for some reason, I built the libs using the buildlib.bat
provided.
> Here's a snip of the code that fails:
>
> int pthread_rdwr_wunlock_np (
>    pthread_rdwr_t *rdwrp
> ){
>    assert( pthread_mutex_lock(&(rdwrp->mutex)) == 0 );
>    if (rdwrp->writers_writing == 0) {
>       assert( pthread_mutex_unlock(&(rdwrp->mutex)) == 0 );
>       return(-1);
>    }else{
>       rdwrp->writers_writing = 0;
>       assert(pthread_cond_broadcast(&(rdwrp->lock_free)) == 0);
>
>    };
>
>    assert( pthread_mutex_unlock(&(rdwrp->mutex)) == 0 );
>    return(0);
> };
>
> pthread_cond_broadcast returns EINVAL. Does anyone recognise this
> problem?
>
> Wkr
> Michael Ambrus
> Siemens Elema
> >
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* pthread_cond_timedwait
@ 1999-09-09  8:51 Mikael.Ambrus
  1999-09-09  9:21 ` pthread_cond_timedwait Scott Lightner
  0 siblings, 1 reply; 7+ messages in thread
From: Mikael.Ambrus @ 1999-09-09  8:51 UTC (permalink / raw)
  To: pthreads-win32

	[Ambrus Mikael]  Dear pthreads colleagues, 

	I'm writing a program that uses pthread_cond_timedwait. In the book
that I'm using (Pthreads Programming by Nicols, Buttlar & Proulux Farell )
it says that this function should suspend the thread until some other thread
calls  pthread_cond_signal, pthread_cond_broadcast OR the system timer is
greater than or equal to the third argument (abstime). 

	Since clock_gettime is not implemented and I cant find some other
function that tells me the system time, I've tried to implement my own
version of clock_gettime by using the ansi function clock(). This function
returns the number of ticks that has elapsed since the program was started.

	But it seams that this is not the same absolute time that
pthread_cond_timedwait requires since the thread won't awaken.

	Is there another way to acquire the relevant system time?

	Another thing that has bothered me is that I recently downloaded the
latest snip of phtreads and now my read/write locks won't work. Since the
precompiled lib isn't recognised by the linker (neither with gcc v20.1 nor
MSVC 6.0) for some reason, I built the libs using the buildlib.bat provided.
Here's a snip of the code that fails:

	int pthread_rdwr_wunlock_np (
	   pthread_rdwr_t *rdwrp
	){
	   assert( pthread_mutex_lock(&(rdwrp->mutex)) == 0 );
	   if (rdwrp->writers_writing == 0) {
	      assert( pthread_mutex_unlock(&(rdwrp->mutex)) == 0 );
	      return(-1);            
	   }else{
	      rdwrp->writers_writing = 0;
	      assert(pthread_cond_broadcast(&(rdwrp->lock_free)) == 0);

	   };

	   assert( pthread_mutex_unlock(&(rdwrp->mutex)) == 0 );
	   return(0);
	};

	pthread_cond_broadcast returns EINVAL. Does anyone recognise this
problem?

	Wkr
	Michael Ambrus
	Siemens Elema
>  

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-06-11  0:56 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-10 18:07 pthread_cond_timedwait Allan Comar
2005-06-10 20:23 ` pthread_cond_timedwait Steve Croall (TIBCO)
2005-06-11  0:56   ` pthread_cond_timedwait Ross Johnson
  -- strict thread matches above, loose matches on Subject: below --
1999-09-09 10:51 pthread_cond_timedwait Bossom, John
1999-09-09  9:39 pthread_cond_timedwait Medina, Aurelio
1999-09-09  8:51 pthread_cond_timedwait Mikael.Ambrus
1999-09-09  9:21 ` pthread_cond_timedwait Scott Lightner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).