Re: Replacment for pthread_mutex_timedlock()?
Re: Replacment for pthread_mutex_timedlock()?
- Subject: Re: Replacment for pthread_mutex_timedlock()?
- From: Lawrence Gold <email@hidden>
- Date: Mon, 30 Apr 2007 13:17:15 -0600
On Apr 30, 2007, at 12:40 PM, George Lawrence Storm wrote:
I am porting an existing Linux library to 10.4, they are using
pthread_mutex_timedlock(), it is not in in pthreads.h on the mac.
Is there an equivalent call on OS X 10.4?
You should be able to implement it roughly like so:
int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex, const
struct timespec *restrict abs_timeout)
{
int result;
do
{
result = pthread_mutex_trylock(&mutex);
if (result == EBUSY)
{
timespec ts;
ts.tv_sec = 0;
ts.tv_sec = 10000000;
/* Sleep for 10,000,000 nanoseconds before trying again. */
int status = -1;
while (status == -1)
status = nanosleep(&ts, &ts);
}
else
break;
}
while (result != 0 and (abs_timeout is 0 or the timeout time has
passed));
return result;
}
The condition for while() is left as an exercise to the reader. ;-)
You can probably use gettimeofday(), but keep in mind that its
timeval is based on microseconds, while timespec is based on
nanoseconds, so you'll need to convert. Also, I'm assuming here that
a timeout of 0 represents an infinite timeout; if that's the case for
you, it might be more efficient to check for this and just call
pthread_mutex_lock().
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden