LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2005, 07:22 PM   #1
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
timeout for a mutex lock


Is it possible to timeout a mutex lock in posix?

i have mutexs etc in classes, but would this psudeo type code work in c++ as part of the mutex class?

Code:
pthread_mutex_t m;

bool get_lock(time_out)
{
        get the systemtime
        wait = systemtime + time_out;
        volitile bool got_mutex = false;

        while (systemtime < wait && got_mutex == false) 
        {
                if ( mutex->trylock(&m) == EBUSY)
                {
                        get the systemtime;
                }
                else 
                {
                        got_mutex = true;
                }
        }
        return got_mutex;
        
}
any thoughts on this?

cheers

Last edited by dmail; 10-23-2005 at 09:05 PM.
 
Old 10-23-2005, 07:37 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Sounds very dangerous to me. If you return true and let somebody go off and mess with some sort of shared memory you could end up with some pretty hard to track bugs. It would be better if you just designed your program so it never had a deadlock and the whole timeout would be a moot point.
 
Old 10-23-2005, 07:46 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Original Poster
Rep: Reputation: Disabled
creating the app without deadlocks is what im trying to do, by removing the block on the mutex this ensures there will never be a deadlock. win32 and osx have this feature in there mutex and im just trying to apply it to posix aswell so that my abstract base class(for osx, win and nix) works for all.

if this is not a good way of doing it, is there some flag etc that i can change the blocking time with?

Last edited by dmail; 10-23-2005 at 07:49 PM.
 
Old 10-23-2005, 08:01 PM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Well, all I'm saying is this kind of thing allows your program to disguise a potential major problem. Id rather know that my logic was causing a thread to never release a mutex lock then to think it works and find out later one of the threads wasn't really working properly.
 
Old 10-24-2005, 06:30 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Original Poster
Rep: Reputation: Disabled
im a little confused jt, i see what you are saying but can you see a way it would return true yet not having gained the lock?

Last edited by dmail; 10-24-2005 at 06:34 AM.
 
Old 10-24-2005, 07:04 AM   #6
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Eh.. apparently I'm an idiot I somehow thought you had the default value of the return as true... As for you logic... I do believe that would work.
 
Old 10-24-2005, 07:22 AM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Original Poster
Rep: Reputation: Disabled
Quote:
Originally posted by jtshaw
Eh.. apparently I'm an idiot I somehow thought you had the default value of the return as true... As for you logic... I do believe that would work.
lol thanks i thought i was missing something, i wouldnt say idiot, coz that would be someone who thought they were wrong but wouldn't admit it
i know its not the most efficient way(well maybe it is coz i dont know another) but i just wanted to give all os's the same funcs. thanks
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
E: Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily unavailable) phreakshew Linux - Newbie 24 04-12-2019 10:42 AM
Caps Lock and Num Lock leds dont work! npc Linux - Hardware 2 11-08-2005 10:40 AM
How does pthread_mutex_lock() lock mutex in pthread icoming Programming 0 12-04-2004 08:54 AM
Num, Scroll, Caps/Lock all Lock up Slackware Zamochit Linux - Newbie 0 10-18-2004 07:47 PM
RedHat 9.0 freezes with blinking Caps lock and Scroll lock queen-bee Linux - Software 0 07-30-2004 10:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration