timeout for lockmutex()?

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

timeout for lockmutex()?

Post by jassing »

I have encountered an issue -- with rapid lock/unlock in multiple threads, the program eventually hangs waiting for a lock.
Absolutely, any lockmutex is countered by unlock.. w/o a dobut.
Is there a way to timeout the mutex?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: timeout for lockmutex()?

Post by Trond »

Absolutely, any lockmutex is countered by unlock.. w/o a dobut.
No, if that was the case, they mutex wouldn't hang. The whole point of the mutexes is that they work correctly with rapid lock/unlock from multiple threads. The bug must be in your program.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: timeout for lockmutex()?

Post by jassing »

Right -- ok; bug in my program; but that doesn't answer the question...


UPDATE: I made 1 change - I said "use the standalone debugger" -- recompiled, program works as expected (no hangs).
Reset the compiler to "built in" -- and the hang happens again.
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: timeout for lockmutex()?

Post by buddymatkona »

Have you verified correct operation with no debug code? The usual way is to put a counter in each thread, activate the thread a large number of times and see if counters between caller and "callee" drift apart.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: timeout for lockmutex()?

Post by jassing »

buddymatkona wrote:Have you verified correct operation with no debug code? The usual way is to put a counter in each thread, activate the thread a large number of times and see if counters between caller and "callee" drift apart.
I have verified that for every Lock there is an unlock.
I have verified this at least 3 different ways.
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: timeout for lockmutex()?

Post by buddymatkona »

In that case, you may have run into the same problem I did.
http://www.purebasic.fr/english/viewtop ... 13&t=44565

If you run code from http://www.purebasic.fr/english/viewtop ... 13&t=44565
in several parallel threads, you will see the default PB Debugger using bad addresses. ( A dual processor might not be enough to reproduce this problem.) You will need to make a small change to get the code to run in parallel with debug code added.
In "Macro ThreadLoop" :
"CompilerIf #PB_Compiler_Debugger = 0" should be changed to "CompilerIf #PB_Compiler_Debugger <> 0"
Then compile with Debugger.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: timeout for lockmutex()?

Post by jassing »

Thanks.

Cheers
-j
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: timeout for lockmutex()?

Post by Trond »

Does the program run ok without the debugger?
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: timeout for lockmutex()?

Post by jassing »

Trond wrote:Does the program run ok without the debugger?
I don't recall right now - as I was focused on debugging the issue -- but I tend to think it wasn't.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: timeout for lockmutex()?

Post by cas »

Did you enable 'Threadsafe' compiler option?
And there is command called TryLockMutex() which you can call in loop and every time it fails then add small Delay(), at end of loop calculate total time wasted in loop and Break if desired time has passed (= timeouted).
GBeebe
Enthusiast
Enthusiast
Posts: 263
Joined: Sat Oct 09, 2004 6:52 pm
Location: Franklin, PA - USA
Contact:

Re: timeout for lockmutex()?

Post by GBeebe »

@cas, I also over looked TryLockMutex() while I was wondering if there was a way to go ahead and do other stuff until the mutex was unlocked. Good tip.

I don't think a program should 'hang' at LockMutex() because (if I understand correctly) that request would get in line and wait it's turn, instead of constantly trying over and over until successful. I have noticed, before learning how to use mutexes, a program that I wrote with threads (accessing the same global variables) and without mutexes, seemingly wouldn't crash at all or would run longer with the debugger on. With the debugger off or compiled, it crashed almost instantly. Don't know why that is. But My guess is that somewhere in your program, there is a line that's trying to access to a global or shared variable (that is used in the threads) and isn't checking the mutex.

As far as debugging output, the code for the CreateMutex() example uses a Console to display the count instead of using the debug window. Maybe they know something we don't?
Post Reply