Logo

Programming-Idioms

  • Rust
  • Perl

Idiom #185 Execute function in 30 seconds

Schedule the execution of f(42) in 30 seconds.

use threads;
my $thr = threads->create(sub { sleep(30); f(42); });
$thr->join()

join() waits for $thr to finish
local $SIG{ALRM} = sub { f(42) };
alarm 30;

Attach a handler to an alarm signal. Then ask the OS to deliver the signal in thirty seconds. NOTE: can handle only one delayed task at a time.
use threads;
my $thr = threads->create(sub { sleep(30); f(42); });
$thr->detach();

Uses Perl 5.6 threads. Note that if the main program exits without having join()ed the thread, the thread will be killed.
use std::time::Duration;
use std::thread::sleep;
sleep(Duration::new(30, 0));
f(42);
(do (Thread/sleep 30000)
    (f 42))

Clojure is a hosted language, and relies on host interop for things like this

New implementation...
< >
programming-idioms.org