RT-Thread- two ways to create threads

Outline

Create a thread three elements:
1. thread stack
2. Thread Control Block
3. Thread the main function

There are ways RTT creating threads in two types:

  • Static creation
  • Dynamically created

From the literal meaning is quite easy to understand both methods, the static creation is to apply a little better space in advance, is automatically created dynamically allocate space according to the required space.

Static creation

The so-called static creation, is to advance to the thread portion of the memory space is divided in SRAM, the memory and all the threads that are related to a piece of memory space. Here simulation creates a thread lit led.

Defined thread stacks

RTT, each thread is independent, so each thread has its own separate runtime environment, the thread of the operating environment are stored in its stack space. Stack occupied by the thread it is inside the MCU RAM, so the MCU can support a number of threads, to see how much RAM it up.
Thread stack functions defined as follows:

/* 栈空间地址对齐 */
ALIGN( RT_ALIGN_SIZE )
/* 定义一个数组,栈的空间大小就是1024*8字节 */
static rt_uint8_t rt_led_thread_stack[1024];

Defined thread function

Thread is actually an infinite loop without C band return value of the function, the thread function is defined as follows:

static void led_thread_entry( void *parameter )
{
	LED_ON;
	rt_thread_delay( 500 );

	LED_OFF;
	rt_thread_delay( 500 );
}

Thread function allows LED to flash at intervals of 500 tick.

Thread Control Block is defined

Thread control block as thread ID, which contains all the information threads.

static struct rt_thread led_thread

Initialize the thread

Three elements thread: thread stack, the thread function, the thread control block, are linked together by a thread initialization function, through the initialization function thread, the thread may be to start the system. (After initialization, the thread is in the ready state)

rt_thread_init(&led1_thread, /* 线程控制块 */ 
 "led1", /* 线程名字 */ 
 led1_thread_entry, /* 线程入口函数 */ 
 RT_NULL, /* 线程入口函数参数 */ 
 &rt_led1_thread_stack[0], /* 线程栈起始地址 */ 
 sizeof(rt_led1_thread_stack), /* 线程栈大小 */ 
 3, /* 线程的优先级 */ 
 20); /* 线程时间片 */

Start thread function

rt_thread_startup(&led_thread);

By rt_thread_startup()function to start a thread

Dynamically created

When the static create a thread, the thread control block and thread stack memory space is allocated from the internal SRAM inside, which address specific allocation to the decision by the compiler used for dynamic creation of dynamic memory - heap that also belong to the SRAM , a large array can be defined in the SRAM for dynamic memory allocation function using the RTT.

Dynamic memory, thread stack when the thread creation created, i.e. the thread stack according to size to create a thread to a given, thread creation function will return a pointer to the thread control block of the thread, so to pre-define a thread control block pointer.

/* 定义线程控制块指针 */
static rt_thread_t led_thread = RT_NULL;

When a thread is created dynamically with a rt_thread_create()function that returns a pointer to the thread control block.

Both in the main function:

int main(void)
{	
	
/**************************** 静态创建 **************************************/	
    ///* 线程初始化函数,将线程栈空间、线程函数、线程控制块 三者联系在一起 */
rt_thread_init( &led1_thread,  //线程控制块指针
                "led1",
								led1_thread_entry,
								RT_NULL,
								&rt_led1_thread_stack[0],//静态创建线程时需要线程栈的起始地址,动态创建时会根据提供的线程栈大小自动创建
								sizeof( rt_led1_thread_stack ),
								3,//0的优先级最高
								20//当有相同优先级的俩个线程时,它们的运行时间由时间片的大小决定
								);

								
						
/* 启动线程,开始调度 */
/* 在这之前线程还处于初始态,需要将线程转换为 就绪态 才可以进行调度,通过rt_thread_startup()函数实现线程的就绪态转换 */
rt_thread_startup( &led1_thread );	
								
/***************************** 动态创建 ******************************************/
/* 利用rt_thread_create() 函数动态创建线程,会返回线程控制块指针 */
led1_thread = rt_thread_create( "led1",
								                led1_thread_entry,
								                RT_NULL,
								                512,
								                3,
								                20);								
								
if( led1_thread != RT_NULL )
	/* rt_thread_startup() 的形参是一个线程控制块指针,动态创建线程时返回的就是线程控制块指针,所以直接传入即可 */
	rt_thread_startup( led1_thread );
else
	return -1;


Published 62 original articles · won praise 188 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43743762/article/details/103113654