The concept of threading is used in Rt-thread instead of tasks. The two are similar, and I'm here to take his thread as a task to understand
1 , Task handling :
Dynamic task-related APIs
Create task: Rt_thread_create function that returns the task ID of the rt_thread_t type after the task is created
Start task after creation: Rt_thread_startup
Delete Task: Rt_thread_delete
Task delay function: Rt_thread_delay delay time, task is in suspend state
Task operation can be done using the finish module and viewed at the computer terminal
2. Dynamic creation of threads and static creation of threads
Both static and dynamic definitions are supported in Rt-thread. Using threads for example, the rt_thread_init corresponds to a static definition, and the rt_thread_create corresponds to the dynamic definition mode.
- When using statically defined methods, you must first define a static thread control block, define the stack space, and then call Rt_thread_init to complete the initialization of the thread. In this way, the memory occupied by the thread control block and the stack is placed in the RW segment, which is determined at compile time that it is not dynamically allocated, so it cannot be freed, and can only be detached from the object manager using the Rt_thread_detach function.
- When using dynamic definition mode rt_thread_create, Rt-thread dynamically requests thread control blocks and stack space. At compile time, the compiler is not aware of this space, only when the program is running, Rt-thread from the system heap to allocate the memory space, when it is not necessary to use the thread, the call to the Rt_thread_delete function will re-release the memory space of the request to the memory heap.
These two ways have pros and cons, static definition mode will occupy rw/zi space, but do not need to allocate memory dynamically, run time is more efficient, good real-time. The dynamic mode does not take up extra rw/zi space and takes up little space, but the runtime needs to allocate memory dynamically, and the efficiency is not high in static mode.
To create a thread code statically:
To create a thread: rt_thread_init
To start a thread: rt_thread_startup
Detach Thread: rt_thread_detach
//Create a thread staticallyresult = Rt_thread_init (&thread1,//Thread Handle "Static",//Thread NameRt_init_thread_entry,//Thread Entry functionRt_null,//Thread Entry Parameters&thread1_stack[0],//Thread Stack Address sizeof(Thread1_stack),//Thread Stack size 6,//Thread Priority Ten);//Thread time slices
To dynamically create thread code:
To create a thread: rt_thread_create
启动线程:rt_thread_startup
线程启动成功后,当OS调度开始,即可被OS调度执行。
Tid = Rt_thread_create ("init", rt_init_thread_entry, Rt_null, 20485); if (tid = rt_null) rt_thread_startup (TID);
Space usage Comparison
Dynamically created threads, which will free up space after delete
A statically created thread that does not free up space after detach
Rt-thread Thread (Task) processing "Rt-thread Learning Note 2"