// E07-Alarm.cpp -- demonstration of simple multi-threaded execution

#include <Platform.h>
using namespace Platform;

// sound the alarm after 3 seconds
#define Timeout 3

// MyAlarm implements Thread::IRunnable interface
class MyAlarm:
    public Thread::IRunnable
    // number of milliseconds until the alarm goes off (timeout)
    Int millisToAlarm;

    // simple constructor
    MyAlarm(Int seconds):
        millisToAlarm(seconds * 1000)
    // run is invoked once the alarm task is created
    Void run()
        // suspend this thread for the specified amount of time
        // print a message from this thread
        Runtime::StdOut::fprintln(appText("Alarm sounded in thread [%Lx]"), Thread::id());
        // once this method returns this thread will end, and the joined (main) thread will continue

// program entry point
    // print a message from the main thread
    Runtime::StdOut::fprintln(appText("Creating an alarm in thread [%Lx] with timeout of %d seconds"), Thread::id(), Timeout);

    // print the current time
    Runtime::StdOut::println(DateTime::now().format(appText("The time is %h:%m:%s.%u")));

    // create an alarm
    MyAlarm alarm(Timeout);
    // start the alarm in a new thread (task)
    Thread::Task task(&alarm);
    // suspend this thread until the alarm task finishes

    // print the new time
    Runtime::StdOut::println(DateTime::now().format(appText("And now the time is %h:%m:%s.%u")));

    return 0;

/* EOF */