Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

threads/threads.c

This sample demonstrates Nut/OS multithreading.

Each thread is started with 192 bytes of stack. This is very low and doesn't provide much space for local variables.

#include <stdio.h>
#include <io.h>

#include <dev/uartavr.h>
#include <sys/thread.h>
#include <sys/timer.h>

/*
 * High priority thread.
 */
THREAD(Thread1, arg)
{
    /*
     * Endless loop in high priority thread.
     */
    NutThreadSetPriority(16);
    for (;;) {
        putchar('H');
        NutSleep(125);
    }
}

/*
 * Low priority thread.
 */
THREAD(Thread2, arg)
{
    /*
     * Endless loop in low priority thread.
     */
    NutThreadSetPriority(128);
    for (;;) {
        putchar('L');
        NutSleep(125);
    }
}

/*
 * Main application thread. 
 */
int main(void)
{
    u_long baud = 115200;

    /*
     * Register the UART device, open it, assign stdout to it and set 
     * the baudrate.
     */
    NutRegisterDevice(&devUart0, 0, 0);
    freopen("uart0", "w", stdout);
    _ioctl(_fileno(stdout), UART_SETSPEED, &baud);

    puts("\nThread Test");

    /*
     * Start two additional threads. All threads are started with 
     * priority 64.
     */
    NutThreadCreate("t1", Thread1, 0, 192);
    NutThreadCreate("t2", Thread2, 0, 192);

    /*
     * Endless loop in main thread.
     */
    for (;;) {
        putchar('M');
        NutSleep(125);
    }
}

© 2000-2003 by egnite Software GmbH - visit http://www.ethernut.de/