RT-Thread学习笔记 —— 线程管理

创建和删除线程(动态线程)

创建线程

1
2
3
4
5
6
7
8
rt_thread_t rt_thread_create(const char* name,                  //线程名称
void (*entry) (void* parameter), //线程入口函数
void* parameter, //线程入口函数参数
rt_uint32_t stack_size, //线程栈大小,单位是字节
rt_uint8_t priority, //线程优先级
rt_uint32_t tick); //线程时间片大小

-> 线程创建成功返回线程句柄,失败返回RT_NULL

删除线程

1
2
3
4
5
rt_err_t rt_thread_delete(
rt_thread_t thread //要删除的线程句柄
);

-> 成功返回RT_EOK,失败返回-RT_ERROR

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <rtthread.h>

#define THREAD_PRIORITY 25
#define THREAD_STACK_SIZE512 512
#define THREAD_TIMESLICE 5

static rt_thread_t tid1 = RT_NULL;

static void thread1_entry(void *parameter)
{
rt_uint32_t count = 0;
while(1)
{
rt_kprintf("thread1 count: %d\n", count ++);
rt_thread_mdelay(500);
}
}

int thread_sample(void)
{
tid1 = rt_thread_create("thread1",
thread1_entry,
RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY,
THREAD_TIMESLICE);

if(tid1 != RT_NULL)
rt_thread_startup(tid1);

return 0;
}

初始化和脱离线程(静态线程)

初始化线程

1
2
3
4
5
6
7
8
9
10
rt_err_t rt_thread_init(struct rt_thread* thread,               //线程句柄
const char* name, //线程名称
void (*entry) (void* parameter), //线程入口函数
void* parameter, //线程入口函数参数
void* stack_start, //线程栈起始地址
rt_uint32_t stack_size, //线程栈大小
rt_uint8_t priority, //线程优先级
rt_uint32_t tick); //线程时间片大小

-> 成功返回RT_EOK,失败返回-RT_ERROR

脱离线程

1
2
3
4
5
rt_err_t rt_thread_detach(
rt_thread_t thread //要脱离的线程句柄
);

-> 成功返回RT_EOK,失败返回-RT_ERROR

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <rtthread.h>

#define THREAD_PRIORITY 25
#define THREAD_STACK_SIZE512 512
#define THREAD_TIMESLICE 5

static struct rt_thread thread2;
static char thread2_stack[1024];

static void thread2_entry(void *parameter)
{
rt_uint32_t count = 0;
while(1)
{
rt_kprintf("thread1 count: %d\n", count ++);
rt_thread_mdelay(500);
}
}

int thread_sample(void)
{
rt_thread_init(&thread2,
"thread2",
thread2_entry,
RT_NULL,
&thread2_stack[0],
sizeof(thread2_stack),
THREAD_PRIORITY,
THREAD_TIMESLICE);

rt_thread_startup(&thread2);
return 0;
}

启动线程

1
2
3
rt_err_t rt_thread_startup(rt_thread_t thread);

-> 成功返回RT_EOK,失败返回-RT_ERROR

获得当前线程

1
2
3
rt_thread_t rt_thread_self(void);

-> 成功返回当前运行的线程句柄,失败返回RT_NULL(调度器未启动)

使线程让出处理器资源

1
2
3
rt_err_t rt_thread_yield(void);

rt_schedule()

使线程睡眠

1
2
3
4
5
rt_err_t rt_thread_sleep(rt_tick_t tick);

rt_err_t rt_thread_delay(rt_tick_t tick);

rt_err_t rt_thread_mdelay(rt_uint32_t ms);

挂起和恢复线程

挂起线程

1
2
3
rt_err_t rt_thread_suspend(rt_thread_t thread);

-> 成功返回RT_EOK,失败返回-RT_ERROR

恢复线程

1
2
3
rt_err_t rt_thread_resume(rt_thread_t thread);

-> 成功返回RT_EOK,失败返回-RT_ERROR

控制线程

1
2
3
4
5
6
7
8
9
10
rt_err_t rt_thread_control(rt_thread_t thread,              //线程句柄
rt_uint8_t cmd, //控制命令
void* arg); //控制参数

-> 成功返回RT_EOK,失败返回-RT_ERROR

支持如下cmd命令:
RT_THREAD_CTRL_CHANGE_PRIORITY //动态更改线程优先级
RT_THREAD_CTRL_STARTUP //开始运行一个线程
RT_THREAD_CTRL_CLOSE //关闭一个线程

设置和删除空闲钩子

1
2
3
4
5
6
rt_err_t rt_thread_idle_sethook(void (*hook)(void));

rt_err_t rt_thread_idle_delhook(void (*hook)(void));

-> 成功返回RT_EOK,失败返回-RT_EFULL
-> hook:设置的钩子函数

设置调度器钩子

1
2
3
4
void rt_scheduler_sethook(void (*hook)(struct rt_thread* from, struct rt_thread* to));

-> 钩子函数声明如下:
void hook(struct rt_thread* from, struct rt_thread* to);