一个线程有几个threadlocal_thread线程

一个线程有几个threadlocal_thread线程基本概念程序是指令的有序集合,其本身没有任何运行的含义,是一个静态的概念。而进程是程序在处理机上的一次执行过程,它是一个动态的概念。进程是由程序、数据和进程控制块三部分组成的。进程是操作系统资源分配的基本单位,而线程是任务调度和执行的基本单位。每个进程都有独立的代码和数据空间(程序上下文),程序之间的切换会有较大的开销;线程可以看做轻量级的进程,同一类线程共享代码和数据空间,每个线程都有自己…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

基本概念

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

线程控制块

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

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;

Jetbrains全家桶1年46,售后保障稳定

意义
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指向双向链表头部

在这里插入图片描述

就绪队列

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

#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]

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.net/204017.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Object类11种方法

    Object类11种方法Object 类 11 种方法 部分方法讲解

    2025年6月18日
    0
  • 正则匹配数组全部是数字[通俗易懂]

    正则匹配数组全部是数字[通俗易懂]var value=[“2″,”3”]   varv=value.join(“”);varreg=/^\d+$/g;if(!v.match(reg)){//是否匹配到了数字,未匹配到就不是return;}if(v.match(reg)[0].lengthreturn;}

    2022年6月17日
    59
  • Spring jar包下载

    Spring jar包下载Spring的下载及目录结构Spring的第一个版本是在2004年发布的,经过10多年的发展,Spring的版本也在不断地升级优化中。Spring开发所需的JAR包分为两个部分,具体如下。1、Spring框架包Spring4.3.6版本的框架压缩包,名称为spring-framework-4.3.6.RELEASE-dist.zip,此压缩包可以通过地址”https…

    2022年5月14日
    32
  • 求生之路2ping高_DDS信号源

    求生之路2ping高_DDS信号源问答时间:2020年12月17日嘉宾简介:高少星:萌宝集团创始人、稻荷资本创始合伙人、《好玩的书》作者。曾任顺为资本董事总经理、百度高级投资经理,是好大夫、丁香园、一点资讯、宝宝巴士、I…

    2025年7月17日
    0
  • ws激活码【中文破解版】2022.01.30

    (ws激活码)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月31日
    50
  • pytest运行_怎么清理ios文件app的缓存

    pytest运行_怎么清理ios文件app的缓存前言pytest运行完用例之后会生成一个.pytest_cache的缓存文件夹,用于记录用例的ids和上一次失败的用例。方便我们在运行用例的时候加上–lf和–ff参数,快速运行上一

    2022年8月6日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号