Go Back   ZeroC Forums > Patches

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
  #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: 6
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
  #2 (permalink)  
Old 03-03-2008
michi's Avatar
michi michi is offline
ZeroC Staff
 
Name: Michi Henning
Organization: ZeroC
Project: Ice
 
Join Date: Feb 2003
Location: Brisbane, Australia
Posts: 880
Have you checked out timed locks in the manual?

Cheers,

Michi.
Reply With Quote
  #3 (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: 6
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.
Reply With Quote
  #4 (permalink)  
Old 03-03-2008
michi's Avatar
michi michi is offline
ZeroC Staff
 
Name: Michi Henning
Organization: ZeroC
Project: Ice
 
Join Date: Feb 2003
Location: Brisbane, Australia
Posts: 880
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.
Reply With Quote
  #5 (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: 6
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.
Reply With Quote
  #6 (permalink)  
Old 03-03-2008
bernard's Avatar
bernard bernard is offline
ZeroC Staff
 
Name: Bernard Normier
Organization: ZeroC, Inc.
Project: Ice
 
Join Date: Feb 2003
Location: Palm Beach Gardens, FL
Posts: 751
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.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
IceGrid lock up/unresponsive tctimmeh Help Center 8 01-18-2008 04:54 PM
IcePy and The global interpreter lock rc_hz Comments 1 10-17-2006 09:50 AM
IceUtil::Mutex for C# amrufon Help Center 1 09-05-2006 09:35 AM
boost::condiition lock Problem ipek Help Center 4 03-10-2006 02:31 PM
Mutex.h : identifier not found shimrod Help Center 4 09-17-2005 09:13 PM


All times are GMT -4. The time now is 07:32 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
(c) 2008 ZeroC, Inc.