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 <cfg/arch.h>
/* Only devDebug1 supported with AT91 */
#ifdef MCU_AT91R40008
#include <dev/debug.h>
#define DEV_UART devDebug1
#define DEV_UART_NAME "uart1"
#else
#include <dev/usartavr.h>
#define DEV_UART devUsartAvr0
#define DEV_UART_NAME "uart0"
#endif
#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(&DEV_UART, 0, 0);
    freopen(DEV_UART_NAME, "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/