Boost logo

Boost :

From: simon meiklejohn (simon_at_[hidden])
Date: 2005-11-27 04:06:19


A simple example - the following program directs the functions f(), g() and
h()
to be called on the background thread.

// kublaikhan.cpp : basic test of DeferThread.
//

#include "deferthread.h"
#include <iostream>

// tasks for the worker thread
///////////////////////////////////////////////////////////////
void f()
{std::cout << "In Amsterdam" << std::endl;}
void g()
{std::cout << "Did Kublai Khan" << std::endl;}
void h()
{std::cout << "His stately pleasure tram decree"
           << std::endl; }
///////////////////////////////////////////////////////////////
void thread_sleep(int seconds )
{
    boost::xtime delay;
    boost::xtime_get(&delay, boost::TIME_UTC );
    delay.sec += seconds;
    boost::thread::sleep( delay );
}
///////////////////////////////////////////////////////////////
void test_worker( defer::DeferThread& worker,
     const boost::function<void (void)>& func )
{
    // get the worker to call the function it its thread
    worker.Defer(func);
    thread_sleep(3); // arbitrary foreground work
}
///////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
    // DeferThread extends DeferPoint
    // it executes requests on a single bacgkround thread
    defer::DeferThread worker;
    // sleep instead of meaningful foreground work
    thread_sleep(3);
    // do a variety of tasks in the background
    test_worker( worker, f );
    test_worker( worker, g );
    test_worker( worker, h );
    thread_sleep(3);
    worker.Stop(true); // close down
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk