#include #include #include #include #include #include #include #include void *thread1( void* arg ) { void* result = NULL; RT_TASK *task1 = NULL; int counter = 0; /* Create new realtime task */ task1 = rt_task_init_schmod( nam2num("MYTHD1"), /* ID */ 0, /* Priority: 0 is highest. 0x3fffffff is lowest */ 1024, /* Stack size */ 1024, /* Max message size */ SCHED_FIFO, /* Task scheduling policy */ 1 /* Processor affinity */ ); if( NULL == task1 ) { printf("Failed to create task1\n"); return( result ); } /* Make this task realtime */ rt_make_hard_real_time(); start_rt_timer( 0 ); /* Make this task periodic */ rt_task_make_periodic_relative_ns( task1, /* This task */ 0, /* start delay in nanoseconds */ 1e+6 /* period in nanoseconds */ ); /* do realtime job */ while( counter < 10 * 1000 ) { rt_task_wait_period(); ++counter; if( !(counter % 1000) ) { /* This is for demonstration. */ /* You SHOULD NOT call non-realtime function in any realtime task. */ printf("Counter = %d\n", counter ); } } stop_rt_timer(); /* Delete realtime task */ rt_task_delete( task1 ); /* Exit thread */ return( result ); } main() { pthread_t th1; RT_TASK *mainTask = NULL; /* Enable non-root hrt */ rt_allow_nonroot_hrt(); /* Create realtime task */ mainTask = rt_task_init_schmod( nam2num("MAINTH"), /* ID */ 10, /* Priority: 0 is highest. 0x3fffffff is lowest */ 1024, /* Stack size */ 1024, /* Max message size */ SCHED_FIFO, /* Task scheduling policy */ 1 /* Processor affinity */ ); if( NULL == mainTask ) { printf("Failed to create main task.\n"); return( 0 ); } /* Create user thread */ pthread_create( &th1, NULL, thread1, NULL ); pthread_join( th1, NULL ); /* Delete MAINTH */ rt_task_delete( mainTask ); }