前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一个线程有几个threadlocal_thread线程

一个线程有几个threadlocal_thread线程

作者头像
全栈程序员站长
发布2022-11-01 13:04:28
2860
发布2022-11-01 13:04:28
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

基本概念

程序是指令的有序集合,其本身没有任何运行的含义,是一个静态的概念。而进程是程序在处理机上的一次执行过程,它是一个动态的概念。进程是由程序、数据和进程控制块三部分组成的。 进程是操作系统资源分配的基本单位,而线程是任务调度和执行的基本单位。 每个进程都有独立的代码和数据空间(程序上下文),程序之间的切换会有较大的开销;线程可以看做轻量级的进程,同一类线程共享代码和数据空间,每个线程都有自己独立的运行栈和程序计数器(PC),线程之间切换的开销小。 系统在运行的时候会为每个进程分配不同的内存空间;而对线程而言,除了CPU外,系统不会为线程分配内存(线程所使用的资源来自其所属进程的资源),线程组之间只能共享资源。 在rtos threadx中,只有一个程序,运行时可以看做只有一个进程,这个进程下包含多个线程。

在这里插入图片描述
在这里插入图片描述

线程控制块

线程控制块(TCB)用来保持运行时线程状态的数据结构,在线程切换时用来保持线程信息。

代码语言:javascript
复制
typedef  struct TX_THREAD_STRUCT
{ 

/* The first section of the control block contains critical information that is referenced by the port-specific assembly language code. Any changes in this section could necessitate changes in the assembly language. */
ULONG       tx_thread_id;           /* Control block ID */
ULONG       tx_run_count;           /* Thread's run counter */
VOID_PTR    tx_stack_ptr;           /* Thread's stack pointer */
VOID_PTR    tx_stack_start;         /* Stack starting address */
VOID_PTR    tx_stack_end;           /* Stack ending address */
ULONG       tx_stack_size;          /* Stack size */
ULONG       tx_time_slice;          /* Current time-slice */
ULONG       tx_new_time_slice;      /* New time-slice */
/* Define pointers to the next and previous ready threads. */ 
struct TX_THREAD_STRUCT 
*tx_ready_next,      
*tx_ready_previous;
/* Define the port extension field. This typically is defined to white space, but some ports of ThreadX may need to have additional fields in the thread control block. This is defined in the file tx_port.h. */
TX_THREAD_PORT_EXTENSION
/***************************************************************/  
/* Nothing after this point is referenced by the target-specific assembly language. Hence, information after this point can be added to the control block providing the complete system is recompiled. */
CHAR_PTR    tx_thread_name;         /* Pointer to thread's name */
UINT        tx_priority;            /* Priority of thread (0-31)*/
UINT        tx_state;               /* Thread's execution state */
UINT        tx_delayed_suspend;     /* Delayed suspend flag */
UINT        tx_suspending;          /* Thread suspending flag */
UINT        tx_preempt_threshold;   /* Preemption threshold */
ULONG       tx_priority_bit;        /* Priority ID bit */
/* Define the thread's entry point and input parameter. */
VOID        (*tx_thread_entry)(ULONG);
ULONG       tx_entry_parameter;
/* Define the thread's timer block. This is used for thread sleep and timeout requests. */
TX_INTERNAL_TIMER
tx_thread_timer;
/* Define the thread's cleanup function and associated data. This is used to cleanup various data structures when a thread suspension is lifted or terminated either by the user or a timeout. */
VOID        (*tx_suspend_cleanup)(struct TX_THREAD_STRUCT *);
VOID_PTR	tx_suspend_control_block;
struct TX_THREAD_STRUCT
*tx_suspended_next,
*tx_suspended_previous;
ULONG       tx_suspend_info;
VOID_PTR    tx_additional_suspend_info;
UINT        tx_suspend_option;
UINT        tx_suspend_status;
/* Define a pointer for Green Hills use. */
VOID_PTR    tx_eh_globals;
/* Define pointers to the next and previous threads in the created list. */
struct TX_THREAD_STRUCT 
*tx_created_next,    
*tx_created_previous;
} TX_THREAD;

意义

tx_thread_id

线程控制块id

tx_run_count

线程运行计数器

tx_stack_ptr

线程堆栈指针

tx_stack_start

堆栈起始地址

tx_stack_end

堆栈结束地址

tx_stack_size

堆栈大小

tx_time_slice

当前时间片(剩余运行时间)

tx_new_time_slice

新的时间片

tx_ready_next

指向下一个就绪线程指针

tx_ready_previous

指向前一个就绪线程指针

tx_thread_name

线程名字指针

tx_priority

线程优先级 (0-31,0为最高优先级,31最低优先级)

tx_state

线程当前状态

tx_delayed_suspend

线程延迟挂起标志

tx_suspending

线程挂起过程标志,正在挂起

tx_preempt_threshold

抢占门限

tx_thread_entry

入口函数指针

tx_entry_parameter

入口函数参数

tx_thread_timer

线程定时器,用于线程sleep

tx_suspend_cleanup

线程清理函数

tx_suspended_next

指向下一个挂起线程指针

tx_suspended_previous

指向前一个挂起线程指针

tx_created_next

线程created list中,指向下一个线程指针

tx_created_previous

线程created list中,指向前一个线程指针

线程状态

线程包括5中状态:就绪态,挂起态,执行态,中止态,完成态。

在这里插入图片描述
在这里插入图片描述

线程堆栈

线程堆栈用于存储局部变量,函数调用上下文,线程切换上下文等。 堆栈大小和堆栈使用的内存由开发者决定,分配。

在这里插入图片描述
在这里插入图片描述

线程管理链表

线程创建时,TCB会插入到一个双向链表中。_tx_thread_created_ptr指向双向链表头部

在这里插入图片描述
在这里插入图片描述

就绪队列

线程就绪队列由数组和双向链表组成。

代码语言:javascript
复制
#define TX_MAX_PRIORITIES 32
TX_THREAD *     _tx_thread_priority_list[TX_MAX_PRIORITIES];

数组元素有32个,数组元素为指向TCB的指针,每一个tcb指针组成双向链表,tx_ready_next和tx_ready_previous分别指向下一个tcb和前一个tcb。 每个数组元素索引为线程优先级,同一个优先级的所有线程都在同一个数组链表中。

在这里插入图片描述
在这里插入图片描述

就绪优先级位图

就绪优先级位图_tx_thread_priority_map由32位bit表示,某位为1表示对应就绪优先级数组中指针不为NULL,链表中有就绪线程。 bit0对应_tx_thread_priority_list[0], bit1对应_tx_thread_priority_list[1], 。。。 bit31对应_tx_thread_priority_list[31]

代码语言:javascript
复制
ULONG           _tx_thread_priority_map;
在这里插入图片描述
在这里插入图片描述

抢占位图

_tx_thread_preempted_map表示线程抢占是按照tx_preempt_threshold值进行判断抢占的。某bit为1表示,对应优先级线程被抢占,原因是抢占线程的优先级大于被抢占线程的tx_preempt_threshold值。 例如,线程a,tx_priority优先级为14,tx_preempt_threshold门限值为10。 线程b,tx_priority优先级为12,由于12大于线程a的tx_preempt_threshold门限值为10,所以无法抢占线程a。 线程c,tx_priority优先级为9,由于9小于线程a的tx_preempt_threshold门限值为10,所以可以抢占线程a。 并且标记_tx_thread_preempted_map的bit14为1. 注:tx_priority值越小,优先级越大

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/204017.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月23日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基本概念
  • 线程控制块
  • 线程状态
  • 线程堆栈
  • 线程管理链表
  • 就绪队列
  • 就绪优先级位图
  • 抢占位图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档