Re: problem with pthread_mutex_timedlock
- From: loic-dev@xxxxxxx
- Date: 23 Nov 2006 00:49:55 -0800
Hallo Hans,
I want to use this function to lock a resource, but with a timeout
and that is the problem, the function returns immediately after calling,
and if the lock was not free, it returns the error code 110, but I have
no idea what this error code means, I've found nothing in the man-pages
or anywhere else
Can anyone help me?
The Pthreads APIs do not return -1 and do not set errno as most other
UNIX functions, but instead they return directly the error number (0 if
success). You can use the standard function like /strerror()/ to get a
description of what the error means:
The error 110 is ETIMEDOUT, which means that the
/pthread_mutex_timedlock()/ timed out. As mentioned by Larry, this
function expects an *absolute time* for the timeout, not a deltatime.
I have included below a short example that summarize what have been
explained.
Cheers,
Loic.
--
/*
* compile: g++ example.cpp -pthread -lrt
*/
#include <pthread.h>
#include <time.h>
#include <string.h>
#include <iostream>
using namespace std;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int
main()
{
struct timespec abs_time;
// lock the ressource
//
pthread_mutex_lock(&lock);
// abs_time = now + 3sec
//
clock_gettime(CLOCK_REALTIME, &abs_time);
abs_time.tv_sec += 3;
// try to lock the ressource again.
// we shall time out in 3 seconds.
//
int rc = pthread_mutex_timedlock (&lock, &abs_time);
if (rc!=0) {
cout << "pthread_mutex_timelock() returned: " << rc << endl
<< "| " << strerror(rc) << endl;
}
// that's all folks!
//
return 0;
}
.
- References:
- problem with pthread_mutex_timedlock
- From: Hans Juergen Gamauf
- problem with pthread_mutex_timedlock
- Prev by Date: Re: Looking for a extensible shell for debug purpose
- Next by Date: Re: Looking for a extensible shell for debug purpose
- Previous by thread: Re: problem with pthread_mutex_timedlock
- Next by thread: Looking for a extensible shell for debug purpose
- Index(es):
Relevant Pages
|