Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals

TaskScheduler Documentation

Basics

This library is built around the concept of a ::TaskQueue - a container to manage and call groups of tasks.

::TaskQueue's are customisable via policy classes, template parameters and other means, which give you a fair amount of flexibility in specifying how a given task queue will operate.

When creating a TaskQueue, you must supply template arguments to specify the frame-length smoothing policy (if any) and also the task ordering policy.

Policy-based design can be confusing at first, so a good place to start is with the predefined TaskQueue types:

In my code, I find myself using a FrameLockedQueue for rendering and input handling tasks. I then throw all my periodically-called tasks into a MovingAverageQueue.

The FixedLimitQueue could be useful when you have a subset of tasks you want to allocate a resticted amount of time to. (Or if you want your app to have a fairly strict maximum frame length).

CallbackType

New in this release is the ability to use a user defined CallbackType, rather than FastDelegate being hard-coded. This is done by specifying the CallbackType template parameter on a TaskOrderingPolicy.

The major upshot of this is that you can now use (eg.) boost::function or your own callback type. The only (?) restriction on CallbackType is that it is callable with an unsigned long and returns a boolean.

For example, it may define: bool operator()(unsigned long elapsedMs)

The default CallbackType for this scheduler remains FastDelegate, but a separate include is provided so you can easily use boost::function.

Using the scheduler with boost::function opens the door for Python support, which is also included in this release.

Synopsis

C++

Time::initialise();

// set up task queues<br>
FrameLockedQueue frameLockedQueue;

FixedLimitQueue fixedLimitQueue;<br>
fixedLimitQueue.setFrameLimit(20);

// add tasks
frameLockedQueue.addTask(&renderFrame, 1.0f);

fixedLimitQueue.addTask(aiTask, 100, 20);
fixedLimitQueue.addTask(pathfindingTask, 200, 20);
fixedLimitQueue.addTask(animationTask, 20, 0);
// ...

// main loop
unsigned long lastMS = Time::getMilliseconds();
while(1)
{
    // update time, find elapsed time since last frame
    unsigned long nowMS = Time::getMilliseconds();
    unsigned long elapsedMS = nowMS - lastMS;
    lastMS = nowMS;

    // run frame-locked tasks
    frameLockedQueue.step(elapsedMS);

    // run variable tasks
    fixedLimitQueue.step(elapsedMS);
}

Python

import Scheduler

print 'Module contents:'
print dir(Scheduler)

Scheduler.initialise()

frameQueue = Scheduler.createFrameLockedQueue()
limitQueue = Scheduler.createFixedLimitQueue()

def reportFrame(elapsedMs):
    print '=== Frame ==='
    return True

def reportLimit(elapsedMs):
    print 'hi'
    return True

frameQueue.addTask(reportFrame, 0.0)

limitQueue.addTask(reportLimit, 5, 0)
limitQueue.addTask(reportLimit, 10, 10)

for i in range(20):
    frameQueue.step(1)
    limitQueue.step(1)


Copyright (c) 2005, Paul Bridger
All rights reserved.