[RTT routine exercises] 1.2 apart from initialization and detachment of static threads

Source: Internet
Author: User

The stack of the static thread is determined during compilation, so it cannot be dynamically created or deleted by the kernel. A static thread can only use detach to schedule it out of the kernel rather than Delete.

So the stack will be defined in the static thread like this

 
Static rt_uint8_t thread1_stack [512];

This is similar to ucosii, but ucosii does not have a dynamic thread.

I personally feel that dynamic threads are more advantageous and more like PCs. Especially when the SRAM is expanded, the advantage is more obvious.

The original official website has a comprehensive explanation:

 
What is a dynamic thread? What is a static thread? What is the difference between the two? RT-thread supports static and dynamic definitions. For example, rt_thread_init corresponds to the static definition mode, and rt_thread_create corresponds to the dynamic definition mode. When using static definition, you must first define static thread control blocks, define the stack space, and then call rt_thread_init to complete thread initialization. In this way, the memory occupied by the thread control block and stack will be placed in the RW segment. This memory space has been determined during compilation. It cannot be dynamically allocated, so it cannot be released, instead, you can only use the rt_thread_detach function to remove the thread control block from the object manager. When rt_thread_create is used, RT-thread dynamically applies for thread control blocks and stack spaces. When you do not need to use this thread, calling the rt_thread_delete function will release the applied memory space to the memory heap. (If the thread is finished, exit, the system also automatically recycles the thread control block and stack space)

CodeThis is simple. Similar to experiment 1.1, one thread disconnects another thread from the scheduler scheduling.

# Include <rtthread. h> static struct rt_thread thread1; static struct rt_thread thread2; static rt_uint8_t thread1_stack [512]; static rt_uint8_t thread2_stack [512]; /* entry for thread1 */static void thread1_entry (void * parameter) {rt_uint32_t COUNT = 0; while (1) {rt_kprintf ("thread count: % d \ n ", count ++); rt_thread_delay (latency) ;}} static void thread2_entry (void * parameter) {rt_thread_delay (latency * 10); rt_thread_detach (& thread1); rt_thread_delay (10 );} int rt_application_init () {rt_err_t result; Result = rt_thread_init (& thread1, "T1", thread1_entry, rt_null, & thread1_stack [0], sizeof (thread1_stack), 7, 10 ); if (result = rt_eok) rt_thread_startup (& thread1); Result = rt_thread_init (& thread2, "T2", thread2_entry, rt_null, & thread2_stack [0], sizeof (thread2_stack ), 6, 10); If (result = rt_eok) rt_thread_startup (& thread2); Return 0 ;}

Output result:

 
\ |/-RT-thread operating system/| \ 1.1.0 build Aug 10 20122006-2012 copyright by RT-thread teamthread count: 0 thread count: 1 thread count: 2 thread count: 3 thread count: 4 thread count: 5 thread count: 6 thread count: 7 thread count: 8 thread count: 9

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.