b4e has asked for the wisdom of the Perl Monks concerning the following question:

hey again, monks.

surely perl must provide some sort of default 'wait for x miliseconds' function. it's just that i can't find it.
what i wanna do is : (in pseudocode)

call subroutine A
wait x miliseconds
call subroutine B
wait x miliseconds
call subroutine C
wait x miliseconds
etc.

if subroutine A is still running after x mSecs the program should call subroutine B, in paralell.

basically like the
$obj->wait([$timeout])
method from the Win32::IPC module just without waiting for an object to be signalled but just for the timeout.

thanks, b.

Replies are listed 'Best First'.
Re: synchronized sub-routine call
by davido (Cardinal) on Jul 26, 2004 at 16:13 UTC
    sleep has seconds resolution.
    Time::HiRes has fractional seconds resolution.

    Oh, but if you want the previous function to continue running, and then time out, look at signal handlers: $SIG{ALRM}

    See perlipc. Here's an example:

    eval { local $SIG{ALRM} = sub { die "alarm clock restart" }; alarm 10; flock(FH, 2); # blocking write lock alarm 0; }; if ($@ and $@ !~ /alarm clock restart/) { die }

    Dave

      use Time::HiRes qw( usleep ); subone(); usleep (100); #sleep for 100 ms subtwo(); usleep (100); #sleep for 100 ms ...


      -Waswas
      unfortunately flock() won't work on win32.
      i'll have a lock a the corresponding win method
Re: synchronized sub-routine call
by BrowserUk (Patriarch) on Jul 26, 2004 at 19:45 UTC

    I don't think most of us (davido aside), have really attempted to answer the question you asked. In part that is because of the way you asked the question.

    In your example, the wait calls will not be reached until the preceding subroutine call has completed, by which time the wait is doing nothing useful. Leastwise, that is the case in a synchronous environment.

    Since alarm has never been implemented on Win32, that isn't going to solve your problem.

    The solution is to use threads.

    use threads 'async'; sub A{ ... } sub B{ ... } sub C{ ... } async \&A; Win32::Sleep( millis ); async \&B; Win32::Sleep( millis ); async \&C; Win32::Sleep( millis );

    That's almost certainly not the complete solution, but the rest depends upon what your trying to achieve.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re: synchronized sub-routine call
by davorg (Chancellor) on Jul 26, 2004 at 16:14 UTC

    For sub-second intervals, you can use the four argument version of select.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: synchronized sub-routine call
by BrowserUk (Patriarch) on Jul 26, 2004 at 16:18 UTC

    As your on Win32, you can use Win32::Sleep( $milliseconds );. No module required.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re: synchronized sub-routine call
by b4e (Sexton) on Jul 26, 2004 at 16:17 UTC
    thanks for your quick replies.

    b4e
Re: synchronized sub-routine call
by sschneid (Deacon) on Jul 26, 2004 at 16:13 UTC
    perldoc -f sleep

    :)

    -s.
Re: synchronized sub-routine call
by Anonymous Monk on Jul 27, 2004 at 15:06 UTC
    again, i agree that my question was probably not accurate enough. the best way to describe my problem would be a DFD, but oh well...

    the thread thing will probably help me. the problem i have with sleep (Win32::sleep, usleep, etc) is that it blocks other interaction with eg checkbuttons, scales etc in the MainLoop()

    thanks, b.
      Ah, see there's always more to it. If you are in a GUI application, you have an event loop. If you have an event loop, you could schedule your timeout or delay with that event loop rather than using sleep, etc. For example, if you're using tk, then look at the bind method (perldoc Tk::bind).