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

threads/threads.c

This sample demonstrates Nut/OS multithreading.

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

static NUTDEVICE *uart;

/*
 * High priority thread.
 */
THREAD(Thread1, arg)
{
    /*
     * Endless loop in high priority thread.
     */
    NutThreadSetPriority(16);
    for(;;) {
        NutPrintString(uart, "H");
        NutPrintFlush(uart);
        NutSleep(200);
    }
}

/*
 * Low priority thread.
 */
THREAD(Thread2, arg)
{
    /*
     * Endless loop in low priority thread.
     */
    NutThreadSetPriority(128);
    for(;;) {
        NutPrintString(uart, "L");
        NutPrintFlush(uart);
        NutSleep(1000);
    }
}

/*
 * Main application thread. 
 */
THREAD(NutMain, arg)
{
    u_long baud = 115200;

    /*
     * Register the UART device, open it and
     * set the baudrate.
     */
    NutRegisterDevice(&devUart0, 0, 0);
    uart = NutDeviceOpen("uart0");
    NutDeviceIOCtl(uart, UART_SETSPEED, &baud);

    NutPrintString_P(uart, PSTR("TimerTest\r\n"));
    NutPrintFlush(uart);

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

    /*
     * Endless loop in main thread.
     */
    for(;;) {
        NutPrintString(uart, "M");
        NutPrintFlush(uart);
        NutSleep(500);
    }
}


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