Files
STM32_Zephyr_RTOS/apps/multithreaded_01/src/main.c
T
2026-10-19 23:09:38 -07:00

60 lines
2.2 KiB
C

#include <zephyr/kernel.h>
/* Stack sizes are in bytes. Lower priority numbers mean higher priority. */
#define THREAD_A_STACK_SIZE 512
#define THREAD_A_PRIORITY 2
#define THREAD_B_STACK_SIZE 512
#define THREAD_B_PRIORITY 10
/* Reserve Thread A's stack with the alignment required by Zephyr. */
K_THREAD_STACK_DEFINE(thread_a_stack, THREAD_A_STACK_SIZE);
/* Thread A's control block must remain valid after main() returns. */
struct k_thread thread_a_data;
/* Zephyr thread entry functions receive three optional arguments.
* This example passes NULL for all three and does not use them.
*/
void thread_a_entry(void *p1, void *p2, void *p3) {
/* Get the priority of the currently running thread (Thread A). */
printk("Thread_A: I am at high priority, value: %d \n",
k_thread_priority_get(k_current_get()));
}
void thread_b_entry(void *p1, void *p2, void *p3) {
/* Thread B has lower priority than Thread A: 10 versus 2. */
printk("Thread_B: I am at low priority, value: %d \n",
k_thread_priority_get(k_current_get()));
}
int main(void) {
/* Create Thread A at runtime using its reserved control block and stack.
* K_THREAD_STACK_SIZEOF supplies the usable stack size to Zephyr.
* NULL arguments are passed to thread_a_entry; options 0 means defaults.
* K_NO_WAIT makes the thread ready immediately, but the scheduler decides
* when it runs. The returned ID can be used in later thread API calls.
*/
k_tid_t thread_a_id =
k_thread_create(&thread_a_data,
thread_a_stack,
K_THREAD_STACK_SIZEOF(thread_a_stack),
thread_a_entry, NULL, NULL, NULL,
THREAD_A_PRIORITY, 0, K_NO_WAIT);
ARG_UNUSED(thread_a_id);
return 0;
}
/* Define Thread B at compile time, including its stack and control block.
* Zephyr uses this metadata during startup to make the thread ready.
* This is not a function call executed after main: its location here does
* not determine execution order. The scheduler invokes thread_b_entry.
* The NULLs are entry arguments; the last two zeros mean default options
* and zero startup delay (in milliseconds), respectively.
*/
K_THREAD_DEFINE(thread_b_id,
THREAD_B_STACK_SIZE,
thread_b_entry, NULL, NULL, NULL,
THREAD_B_PRIORITY, 0, 0);