Prex Home > Document Index > Sample Codes

Sample Codes

Version 1.0, 2005/09/01

Table of Contents


Running a new thread

This is a sample to create and run a new thread in the same task.

int thread_run(void *start, void *stack, thread_t *th)
{
        thread_t t;
        int err;

        err = thread_create(task_self(), &t);
        if (err)
                 return err;

        err = thread_load(t, start, stack);
        if (err)
                 return err;

        err = thread_resume(t);
        if (err)
                 return err;

        *th = t;
        return 0;
}

Periodic timer

This is a sample to show a message per 100msec starting after 5sec.

int main()
{
        int count = 0;

        sys_log("periodic timer test program\n");

        /* Setup timer */
        timer_periodic(thread_self(), 5000, 100);

        while (count++ < 5) {
                timer_waitperiod();
                sys_log("Hello!\n");
        }

        /* Cancel timer */
        timer_periodic(thread_self(), 0, 0);
        return 0;
}