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>
#ifdef MCU_AT91R40008
#include <dev/debug.h>
#define DEV_UART devDebug1
#define DEV_UART_NAME "uart1"
#elif defined(MCU_GBA)
#include <dev/debug.h>
#define DEV_UART devDebug0
#define DEV_UART_NAME "con"
#else
#include <dev/usartavr.h>
#define DEV_UART devUsartAvr0
#define DEV_UART_NAME "uart0"
#endif
#include <sys/thread.h>
#include <sys/timer.h>
THREAD(Thread1, arg)
{
NutThreadSetPriority(16);
for (;;) {
putchar('H');
NutSleep(125);
}
}
THREAD(Thread2, arg)
{
NutThreadSetPriority(128);
for (;;) {
putchar('L');
NutSleep(125);
}
}
int main(void)
{
u_long baud = 115200;
NutRegisterDevice(&DEV_UART, 0, 0);
freopen(DEV_UART_NAME, "w", stdout);
_ioctl(_fileno(stdout), UART_SETSPEED, &baud);
puts("\nThread Test");
NutThreadCreate("t1", Thread1, 0, 192);
NutThreadCreate("t2", Thread2, 0, 192);
for (;;) {
putchar('M');
NutSleep(125);
}
}