interval_timer


To deliver a signal to the current process after a specified time, use an interval timer. An interval timer can deliver a signal once or periodically. The following are methods an interval timer can use to count time.

The current process owns all of these timers, which are disabled by default. Access these timers by using the interval timer interface of os_this_process ; these timers cannot be constructed directly.

The following example uses an interval timer to limit the duration of a program. The program spawns a child that executes a sleep 10 command and terminates after two seconds.

Example <ospace/unix/examples/itimer1.cpp>
#include <iostream>
#include <ospace/unix.h>

const int delay = 2;
int child_pid = 0;

void real_time_handler( int signal )
  {
  os_process child( child_pid ); // Construct process using child PID.
  if ( child.valid() ) // Check child has not already terminated.
    {
    cout << delay << " seconds has expired: killing " << child << endl;
    child.kill();
    }
  }

int main()
  {
  os_unix_toolkit initialize;

  // Install handler.
  os_this_process::action( SIGALRM, real_time_handler );
  os_interval_timer timer = os_this_process::real_timer();
  timer.timer( delay ); // Start the timer.
  cout << "Timer started for " << delay << " seconds." << endl;
  cout << "  " << timer << endl;

  // Spawn child to sleep for 10 seconds.
  os_process child( "sleep", "10", 0 );
  child_pid = child.pid(); // Store child's PID.

  // Look at child's status to see how it terminated.
  os_process_status status = os_this_process::wait_for_child( child );
  timer.cancel(); // Cancel timer.
  cout << "Child finished with status: " << status << endl;
  return status.exit_code();
  }

Timer started for 2 seconds.
  os_interval_timer( real: 1.996749 seconds )
2 seconds has expired: killing os_process( 4621 )
Child finished with status: os_process_status( 4621 )

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.