Re: problem with pthread_mutex_timedlock



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;
}

.



Relevant Pages

  • Re: problem with pthread_mutex_timedlock
    ... I want to use this function to lock a resource, but with a timeout ... and if the lock was not free, it returns the error code 110, but I have ...
    (comp.os.linux.development.apps)
  • problem with pthread_mutex_timedlock
    ... 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 ...
    (comp.os.linux.development.apps)
  • [PATCH 03/24] GFS2: Something nonlinear this way comes!
    ... mappings. ... to take the lock here at all - just doing file_accessedwould be ... * Returns: 0 or error code ... * a missed atime update. ...
    (Linux-Kernel)
  • Threading newbie: Lock statement
    ... I have two threads accessing the same ressource, ... Can I use the lock statement to synchronise ... public static void MySecondThreadProcedure() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threading newbie: Lock statement
    ... Let me elaborate: I have two threads accessing the same ressource, but from *different* parts of the code. ... Can I use the lock statement to synchronise access, or can I only use lock when the threads access the ressource from the *same* part of he code. ...
    (microsoft.public.dotnet.languages.csharp)