View Single Post
  #1 (permalink)  
Old 03-03-2008
kalaxy kalaxy is offline
Registered User
 
Name: Kalon Mills
Organization: Omniture, Inc.
Project: Distributed/Parallel processing.
 
Join Date: Dec 2007
Posts: 8
Timed Lock for Mutex Class

It would be really great if you could add a timedLock function to the Mutex class. Below is an idea of how it might work for the pthread world.

Code:
inline bool
Mutex::timedLock(const Time& timeout) const
{
    timeval tv = Time::now() + timeout;
    timespec ts;
    ts.tv_sec = tv.tv_sec;
    ts.tv_nsec = tv.tv_usec * 1000;
    int rc = pthread_mutex_timedlock(&_mutex, &ts);

    if (rc != 0 && rc != ETIMEDOUT)
    {
        if(rc == EDEADLK)
        {
            throw ThreadLockedException(__FILE__, __LINE__);
        }
        else
        {
            throw ThreadSyscallException(__FILE__, __LINE__, rc);
        }
    }
    return (rc == 0);
}
I don't know much about the Windows world. Maybe it's overly complex and that's why you haven't already done it.
Reply With Quote