Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^5: AnyEvent: How to protect critical sections?

by Corion (Pope)
on May 17, 2011 at 21:03 UTC ( #905367=note: print w/ replies, xml ) Need Help??


in reply to Re^4: AnyEvent: How to protect critical sections?
in thread AnyEvent: How to protect critical sections?

This cannot work - if you expect ->lock() to block until the semaphore is released, how can it continue to execute the remaining code elsewhere?

I would either use a plain boolean variable, like you did in your first case, and keep the timer running:

my $critical; sub on_timer { if (! $critical) { ... }; };

... or manually restart the timer when it's allowed to restart again. The guard values returned from creating a timer allow you to conveniently stop a timer.


Comment on Re^5: AnyEvent: How to protect critical sections?
Select or Download Code
Replies are listed 'Best First'.
Re^6: AnyEvent: How to protect critical sections?
by saintmike (Vicar) on May 17, 2011 at 21:25 UTC
    This cannot work - if you expect ->lock() to block until the semaphore is released, how can it continue to execute the remaining code elsewhere?
    The same way that $condvar->recv() blocks and lets code run elsewhere.

    Only problem: $condvar->recv() cannot be called from within a callback, according to the AnyEvent docs:

    "Note that doing a blocking wait in a callback is not supported by any event loop, that is, recursive invocation of a blocking "->recv" is not allowed,".

      Indeed, if you would need to do that, you need to use real threads (e.g. Coro) - the limitation of condvars is partially lifted under Coro.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://905367]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (15)
As of 2015-07-30 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls