Here is a simple example program that initializes the library and then starts two threads. The primordial thread plus the two new threads flash LEDs at different rates. All the threads call the example sleep() function which ensures that all the threads will run.
#include <p30fxxxx.h> #include <threads.h> // Hardware-specific configuration _FOSC(CSW_FSCM_OFF & XT_PLL8); _FWDT(WDT_OFF); _FBORPOR(PBOR_ON & BORV_20 & PWRT_64 & MCLR_EN); // Hardware-specific port specifications #define LED_0_TRIS() (_TRISA9 = 0) #define LED_0_OFF() (_LATA9 = 0) #define LED_0_ON() (_LATA9 = 1) #define LED_1_TRIS() (_TRISA10 = 0) #define LED_1_OFF() (_LATA10 = 0) #define LED_1_ON() (_LATA10 = 1) #define LED_2_TRIS() (_TRISA14 = 0) #define LED_2_OFF() (_LATA14 = 0) #define LED_2_ON() (_LATA14 = 1) // Sleep routine void sleep(uint16_t n) { for (uint16_t i = 0; i < n; i++) { for (volatile uint16_t j = 0; j < 1000; j++) ; threads_task_switch(); } } #define STACK_SIZE 64 // Thread 1 configuration thread_data_t thread1_data; uint16_t thread1_stack[STACK_SIZE]; void thread1(void) { // Flash LED 1 forever for (int i = 0; i < 5; i++) { LED_1_ON(); sleep(50); LED_1_OFF(); sleep(50); } } // Thread 2 configuration thread_data_t thread2_data; uint16_t thread2_stack[STACK_SIZE]; void thread2(void) { // Flash LED 2 forever for (int i = 0; i < 5; i++) { LED_2_ON(); sleep(200); LED_2_OFF(); sleep(200); } } int main(void) { // Initialize some LED ports as output ports LED_0_TRIS(); LED_1_TRIS(); LED_2_TRIS(); // Initialize threads library threads_init(); // Start two threads threads_start(&thread1_data, thread1, thread1_stack, STACK_SIZE); threads_start(&thread2_data, thread2, thread2_stack, STACK_SIZE); // Primordial thread flashes LED 0 forever for (;;) { LED_0_ON(); sleep(100); LED_0_OFF(); sleep(100); } }