xorp

TimeSlice Class Reference

A class for computing whether some processing is taking too long. More...

#include <time_slice.hh>

List of all members.

Public Member Functions

 TimeSlice (uint32_t usec_limit, size_t test_iter_frequency)
 Constructor for a given time limit and test frequency.
void reset ()
 Reset the TimeSlice object.
bool is_expired ()
 Test if the time slice has expired.

Private Attributes

TimeVal _time_slice_limit
const size_t _test_iter_frequency
TimeVal _time_slice_start
size_t _remain_iter

Detailed Description

A class for computing whether some processing is taking too long.

This class can be used to compute whether some processing is taking too long time to complete. It is up to the program that uses TimeSlice to check whether the processing is taking too long, and suspend processing of that task if necessary.

Example of usage:

    TimeSlice time_slice(100000, 20); // 100ms, test every 20th iteration
    for (size_t i = 0; i < 1000000000; i++) {
        if (time_slice.is_expired()) {
            // Stop processing.
            // Schedule a timer after 0 ms to continue processing.
            // If needed, save state to continue from here.
            return;
        }
        // Do something CPU-hungry
    }
    time_slice.reset(); // Prepare it again for later usage if needed

Constructor & Destructor Documentation

TimeSlice::TimeSlice ( uint32_t  usec_limit,
size_t  test_iter_frequency 
)

Constructor for a given time limit and test frequency.

Create a TimeSlice object that will measure time slices with given frequency of testing. The time slice is measured once in test_iter_frequency times when method is_expired() is called.

Parameters:
usec_limitthe time slice to measure in microseconds.
test_iter_frequencythe frequency of measuring the time slice.

Member Function Documentation

bool TimeSlice::is_expired ( ) [inline]

Test if the time slice has expired.

If the time slice has expired, automatically prepare this object to start measuring again the time slice.

Returns:
true if the time slice has expired, otherwise false.

The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Enumerations