Results 1 to 6 of 6

Thread: Timed Lock for Mutex Class

  1. #1
    kalaxy is offline Registered User
    Name: Kalon Mills
    Organization: Omniture, Inc.
    Project: Distributed/Parallel processing.
    Join Date
    Dec 2007
    Location
    California
    Posts
    11

    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.

  2. #2
    michi's Avatar
    michi is offline Registered User
    Name: Michi Henning
    Organization: Triodia Technologies
    Project: I have a passing interest in Ice :-)
    Join Date
    Feb 2003
    Location
    Brisbane, Australia
    Posts
    1,055
    Have you checked out timed locks in the manual?

    Cheers,

    Michi.

  3. #3
    kalaxy is offline Registered User
    Name: Kalon Mills
    Organization: Omniture, Inc.
    Project: Distributed/Parallel processing.
    Join Date
    Dec 2007
    Location
    California
    Posts
    11
    Quote Originally Posted by michi View Post
    Have you checked out timed locks in the manual?
    Yes and thanks for pointing it out, but it looks like it is only used for the RWRecMutex. I'll probably end up using the RWRecMutex but I don't need the RW or Rec functionality, which according to your Efficiency Considerations page it has [potentially significant] overhead in size and time.

  4. #4
    michi's Avatar
    michi is offline Registered User
    Name: Michi Henning
    Organization: Triodia Technologies
    Project: I have a passing interest in Ice :-)
    Join Date
    Feb 2003
    Location
    Brisbane, Australia
    Posts
    1,055
    Yes, RWRecMutex has more overhead than a plain mutex. But I suggest that you benchmark your code before rejecting RWRecMutex: unless the critical region you are protecting is very small, and unless you are locking and unlocking that region very frequently, chances are that you will not notice any difference.

    Cheers,

    Michi.

  5. #5
    kalaxy is offline Registered User
    Name: Kalon Mills
    Organization: Omniture, Inc.
    Project: Distributed/Parallel processing.
    Join Date
    Dec 2007
    Location
    California
    Posts
    11
    Quote Originally Posted by michi View Post
    ... chances are that you will not notice any difference.
    Good point. You are probably right that in my case that I won't notice a thing. I guess I was just looking for a theoretically most efficient solution without realizing the practical details.

  6. #6
    bernard's Avatar
    bernard is offline ZeroC Staff
    Name: Bernard Normier
    Organization: ZeroC, Inc.
    Project: Ice
    Join Date
    Feb 2003
    Location
    Palm Beach Gardens, FL
    Posts
    1,294
    Quote Originally Posted by kalaxy View Post
    I don't know much about the Windows world. Maybe it's overly complex and that's why you haven't already done it.
    On Windows, the underlying primitive is a CRITICAL_SECTION, and there is no associated timed-wait function. That's why it's not practical to add timedLock to IceUtil::Mutex.

    Cheers,
    Bernard
    Bernard Normier
    ZeroC, Inc.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Timed out
    By IsraUC3M in forum Help Center
    Replies: 1
    Last Post: 11-26-2010, 01:30 PM
  2. dead lock
    By jerrylucky in forum Help Center
    Replies: 6
    Last Post: 07-25-2008, 09:08 AM
  3. IceGrid lock up/unresponsive
    By tctimmeh in forum Help Center
    Replies: 8
    Last Post: 01-18-2008, 04:54 PM
  4. IcePy and The global interpreter lock
    By rc_hz in forum Comments
    Replies: 1
    Last Post: 10-17-2006, 09:50 AM
  5. boost::condiition lock Problem
    By ipek in forum Help Center
    Replies: 4
    Last Post: 03-10-2006, 02:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •