rt-thread操作系统是一个多线程的操作系统,线程对于rt-thread来说是一个很重要的概念,因此,必须掌握它。
1 线程控制块的数据结构
[cpp]
/**
* Thread structure
*/
struct rt_thread
{
/* rt object *///这里就是rt_object的结构,其实也可以用rt_object parent来定义,估计线程在早些时候并没有这么做,后来也就没改过来
char name[RT_NAME_MAX]; /**< the name of thread */
rt_uint8_t type; /**< type of object */
rt_uint8_t flags; /**< thread's flags */
#ifdef RT_USING_MODULE//模块ID
void *module_id; /**< id of application module */
#endif
//内核对象链表
rt_list_t list; /**< the object list */
rt_list_t tlist; /**< the thread list *///线程链表,一般用作就绪队列元素节点
/* stack point and entry */
void *sp; /**< stack point *///栈指针
void *entry; /**< entry *///入口函数
void *parameter; /**< parameter *///入口函数对应的参数
void *stack_addr; /**< stack address *///栈地址
rt_uint16_t stack_size; /**< stack size *///栈大小
/* error code */
rt_err_t error; /**< error code *///错误代码
rt_uint8_t stat; /**< thread stat *///线程的当前状态
/* priority */
rt_uint8_t current_priority; /**< current priority *///当前优先级
rt_uint8_t init_priority; /**< initialized priority *///初始优先级
#if RT_THREAD_PRIORITY_MAX > 32
rt_uint8_t number;
rt_uint8_t high_mask;
#endif
rt_uint32_t number_mask;
#if defined(RT_USING_EVENT)//与IPC机制事件相关的一些参数
/* thread event */
rt_uint32_t event_set;
rt_uint8_t event_info;
#endif
rt_ubase_t init_tick; /**< thread's initialized tick *///初始tick
rt_ubase_t remaining_tick; /**< remaining tick *///剩余tick
struct rt_timer thread_timer; /**< built-in thread timer *///线程定时器
void (*cleanup)(struct rt_thread *tid); /**< cleanup function when thread exit *///相当于线程的析构函数,用于销毁线程时做些后续操作
rt_uint32_t user_data; /**< private user data beyond this thread *///析构函数的输入参数
};
typedef struct rt_thread *rt_thread_t;
2 线程创建及初始化
2.1 初始化线程
[cpp]
/*@{*/
/**
* This function will initialize a thread, normally it's used to initialize a
* static thread object.
*
* @param thread the static thread object
* @param name the name of thread, which shall be unique
* @param entry the entry function of thread
* @param parameter the parameter of thread enter function
* @param stack_start the start address of thread stack
* @param stack_size the size of thread stack
* @param priority the priority of thread
* @param tick the time slice if there are same priority thread
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
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)
{
/* thread check *///参数检查
RT_ASSERT(thread != RT_NULL); &nb
补充:软件开发 , C++ ,