当前位置: 首页 > news >正文

南阳专业网站建设价格接app推广接单平台

南阳专业网站建设价格,接app推广接单平台,windows2012做网站,nginx做网站参考:RT-Thread API参考手册: 线程管理 线程的分类:动态线程,静态线程 动态线程是系统自动从动态内存堆上分配栈空间的线程句柄(程序运行时再分配空间),静态线程是由用户分配栈空间与线程句柄(可以说是程序编译时已经分配好空间) 1.创建线程 创建一个动态线程 rt_thread_t …

参考:RT-Thread API参考手册: 线程管理

线程的分类:动态线程,静态线程

动态线程是系统自动从动态内存堆上分配栈空间的线程句柄(程序运行时再分配空间),
静态线程是由用户分配栈空间与线程句柄(可以说是程序编译时已经分配好空间)

1.创建线程

创建一个动态线程

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);调用这个函数时,系统会从动态堆内存中分配一个线程句柄以及按照参数中指定的栈大小从动态堆内存中分配相应的空间。分配出来的栈空间是按照 rtconfig.h 中配置的 RT_ALIGN_SIZE 方式对齐。线程创建 rt_thread_create() 的参数和返回值见下表:
参数描述
name线程的名称;线程名称的最大长度由 rtconfig.h 中的宏 RT_NAME_MAX 指定,多余部分会被自动截掉
entry线程入口函数
parameter线程入口函数参数
stack_size线程栈大小,单位是字节
priority线程的优先级。优先级范围根据系统配置情况(rtconfig.h 中的 RT_THREAD_PRIORITY_MAX 宏定义),如果支持的是 256 级优先级,那么范围是从 0~255,数值越小优先级越高,0 代表最高优先级
tick线程的时间片大小。时间片(tick)的单位是操作系统的时钟节拍。当系统中存在相同优先级线程时,这个参数指定线程一次调度能够运行的最大时间长度。这个时间片运行结束时,调度器自动选择下一个就绪态的同优先级线程进行运行
返回——
thread线程创建成功,返回线程句柄
RT_NULL线程创建失败

创建一个静态线程

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);静态线程的线程句柄(或者说线程控制块指针)、线程栈由用户提供。静态线程是指线程控制块、线程运行栈一般都设置为全局变量,在编译时就被确定、被分配处理,内核不负责动态分配内存空间。需要注意的是,用户提供的栈首地址需做系统对齐(例如 ARM 上需要做 4 字节对齐)。线程初始化接口 rt_thread_init() 的参数和返回值见下表:
参数描述
thread线程句柄。线程句柄由用户提供出来,并指向对应的线程控制块内存地址
name线程的名称;线程名称的最大长度由 rtconfig.h 中定义的 RT_NAME_MAX 宏指定,多余部分会被自动截掉
entry线程入口函数
parameter线程入口函数参数
stack_start线程栈起始地址
stack_size线程栈大小,单位是字节。在大多数系统中需要做栈空间地址对齐(例如 ARM 体系结构中需要向 4 字节地址对齐
priority线程的优先级。优先级范围根据系统配置情况(rtconfig.h 中的 RT_THREAD_PRIORITY_MAX 宏定义),如果支持的是 256 级优先级,那么范围是从 0 ~ 255,数值越小优先级越高,0 代表最高优先级
tick线程的时间片大小。时间片(tick)的单位是操作系统的时钟节拍。当系统中存在相同优先级线程时,这个参数指定线程一次调度能够运行的最大时间长度。这个时间片运行结束时,调度器自动选择下一个就绪态的同优先级线程进行运行
返回——
RT_EOK线程创建成功
-RT_ERROR线程创建失败

2.启动线程

创建(初始化)的线程状态处于初始状态,并未进入就绪线程的调度队列,我们可以在线程初始化 / 创建成功后调用下面的函数接口让该线程进入就绪态

rt_err_t rt_thread_startup(rt_thread_t thread);

当调用这个函数时,将把线程的状态更改为就绪状态,并放到相应优先级队列中等待调度。如果新启动的线程优先级比当前线程优先级高,将立刻切换到这个线程。线程启动接口 rt_thread_startup() 的参数和返回值见下表:

参数描述
thread线程句柄
返回——
RT_EOK线程启动成功
-RT_ERROR线程启动失败

3.线程睡眠

在实际应用中,我们有时需要让运行的当前线程延迟一段时间,在指定的时间到达后重新运行,这就叫做 “线程睡眠”。线程睡眠可使用以下三个函数接口:

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_int32_t ms);这三个函数接口的作用相同,调用它们可以使当前线程挂起一段指定的时间,当这个时间过后,线程会被唤醒并再次进入就绪状态。这个函数接受一个参数,该参数指定了线程的休眠时间。线程睡眠接口 rt_thread_sleep/delay/mdelay() 的参数和返回值见下表:
参数描述
tick/ms线程睡眠的时间:
sleep/delay 的传入参数 tick 以 1 个 OS Tick 为单位 ;
mdelay 的传入参数 ms 以 1ms 为单位;
返回——
RT_EOK操作成功

4.线程的删除(对于动态线程)

对于一些使用 rt_thread_create() 创建出来的线程,当不需要使用,或者运行出错时,我们可以使用下面的函数接口来从系统中把线程完全删除掉:

rt_err_t rt_thread_delete(rt_thread_t thread);调用该函数后,线程对象将会被移出线程队列并且从内核对象管理器中删除,线程占用的堆栈空间也会被释放,收回的空间将重新用于其他的内存分配。实际上,用 rt_thread_delete() 函数删除线程接口,仅仅是把相应的线程状态更改为 RT_THREAD_CLOSE 状态,然后放入到 rt_thread_defunct 队列中;而真正的删除动作(释放线程控制块和释放线程栈)需要到下一次执行空闲线程时,由空闲线程完成最后的线程删除动作。线程删除 rt_thread_delete() 接口的参数和返回值见下表:
参数描述
thread要删除的线程句柄
返回——
RT_EOK删除线程成功
-RT_ERROR删除线程失败

注:rt_thread_create() 和 rt_thread_delete() 函数仅在使能了系统动态堆时才有效(即 RT_USING_HEAP 宏定义已经定义了)。

5.线程的脱离(对于静态线程)

对于用 rt_thread_init() 初始化的线程,使用 rt_thread_detach() 将使线程对象在线程队列和内核对象管理器中被脱离。线程脱离函数如下:

rt_err_t rt_thread_detach (rt_thread_t thread);线程脱离接口 rt_thread_detach() 的参数和返回值见下表:
参数描述
thread线程句柄,它应该是由 rt_thread_init 进行初始化的线程句柄。
返回——
RT_EOK线程脱离成功
-RT_ERROR线程脱离失败

这个函数接口是和 rt_thread_delete() 函数相对应的,
rt_thread_delete() 函数操作的对象是 rt_thread_create() 创建的句柄,
rt_thread_detach() 函数操作的对象是使用 rt_thread_init() 函数初始化的线程控制块。
同样,线程本身不应调用这个接口脱离线程本身。

编程实例

/*线程测试*/
//静态线程
struct rt_thread Static_Task; //任务句柄
#define HeadBufSize  512 //栈空间
ALIGN(RT_ALIGN_SIZE)
uint8_t HeadBuf[HeadBufSize]={0};//任务栈空间
//动态线程
rt_thread_t tid = RT_NULL;//静态线程
void Thread_Entry1(void *parameter)
{static int Cnt1 = 0;rt_err_t Err;while(1){		rt_kprintf("task1 cnt =%d\n",Cnt1++);if(Cnt1==2){rt_enter_critical();                   //进入临界区 注意:删除任务要进入临界区,否则有可能删除不了//Err = rt_thread_control(&Static_Task,RT_THREAD_CTRL_CLOSE,RT_NULL);Err = rt_thread_detach(&Static_Task); //静态线程 脱离if(Err==RT_EOK){rt_kprintf("task1 exit\n");}rt_exit_critical();//退出临界区}}
}
//动态线程
void Thread_Entry2(void *parameter)
{static int Cnt2 = 0;rt_err_t Err;while(1){rt_kprintf("task2 cnt =%d\n",Cnt2++);if(Cnt2==2){rt_enter_critical();           //进入临界区  注意:删除任务要进入临界区,否则有可能删除不了Err = rt_thread_delete (tid); //静态线程 脱离if(Err==RT_EOK){rt_kprintf("task2 exit\n");}rt_exit_critical();            //退出临界区}		}}int ThreadTestInit(void)
{rt_err_t Err = 0;__HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE);__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);__HAL_UART_ENABLE_IT(&huart2,UART_IT_IDLE);__HAL_UART_ENABLE_IT(&huart2,UART_IT_RXNE);	//创建静态线程Err = rt_thread_init(&Static_Task,"Task1",Thread_Entry1,RT_NULL,&HeadBuf[0],sizeof(HeadBuf),10,5);if(Err==RT_EOK)rt_thread_startup(&Static_Task);//启动任务	//创建动态线程tid = rt_thread_create("Task2",Thread_Entry2,RT_NULL,512,10,5 );if(tid!=RT_NULL)rt_thread_startup(tid); //启动任务return 0;
}
INIT_APP_EXPORT(ThreadTestInit); //自动初始化

输出结果

 \ | /
- RT -     Thread Operating System
 / | \     3.1.5 build Mar  4 2023
 2006 - 2020 Copyright by rt-thread team
task1 cnt =0
task1 cnt =1
task1 exit
task2 cnt =0
task2 cnt =1
task2 exit


文章转载自:
http://dinncoelba.tqpr.cn
http://dinncoamazedly.tqpr.cn
http://dinncogasolier.tqpr.cn
http://dinncopersuasively.tqpr.cn
http://dinncobrassfounder.tqpr.cn
http://dinncodextrorsely.tqpr.cn
http://dinncovying.tqpr.cn
http://dinncolathing.tqpr.cn
http://dinncobookcraft.tqpr.cn
http://dinncobefittingly.tqpr.cn
http://dinncocorallaceous.tqpr.cn
http://dinncoindisposed.tqpr.cn
http://dinncohorseweed.tqpr.cn
http://dinncofubsy.tqpr.cn
http://dinncoleitmotiv.tqpr.cn
http://dinncolepidocrocite.tqpr.cn
http://dinncodistal.tqpr.cn
http://dinncoferredoxin.tqpr.cn
http://dinncopittsburgh.tqpr.cn
http://dinncosulfa.tqpr.cn
http://dinncoceil.tqpr.cn
http://dinncowashin.tqpr.cn
http://dinncosimonize.tqpr.cn
http://dinncoreconsignment.tqpr.cn
http://dinncolevorotation.tqpr.cn
http://dinncoeblaite.tqpr.cn
http://dinnconiggerize.tqpr.cn
http://dinncosantiago.tqpr.cn
http://dinncoegoistical.tqpr.cn
http://dinncohive.tqpr.cn
http://dinncoicescape.tqpr.cn
http://dinncohelluva.tqpr.cn
http://dinncoconclusive.tqpr.cn
http://dinncoesthetician.tqpr.cn
http://dinncobowyer.tqpr.cn
http://dinncohypnagogic.tqpr.cn
http://dinncodenny.tqpr.cn
http://dinncounshorn.tqpr.cn
http://dinncostipel.tqpr.cn
http://dinncofalkner.tqpr.cn
http://dinncosheepshead.tqpr.cn
http://dinncospringe.tqpr.cn
http://dinncopsychon.tqpr.cn
http://dinncoridgepole.tqpr.cn
http://dinncohetman.tqpr.cn
http://dinncolaughingstock.tqpr.cn
http://dinncojargonaphasia.tqpr.cn
http://dinncoaau.tqpr.cn
http://dinncocrested.tqpr.cn
http://dinncothumper.tqpr.cn
http://dinncosinologue.tqpr.cn
http://dinncowasteweir.tqpr.cn
http://dinncoadulterated.tqpr.cn
http://dinncoviatica.tqpr.cn
http://dinncoramallah.tqpr.cn
http://dinncoomnifocal.tqpr.cn
http://dinnconazareth.tqpr.cn
http://dinncoalegar.tqpr.cn
http://dinncojiggers.tqpr.cn
http://dinncohygroscopic.tqpr.cn
http://dinncocarbonyl.tqpr.cn
http://dinncogynaecologic.tqpr.cn
http://dinncodebarment.tqpr.cn
http://dinncorhotacism.tqpr.cn
http://dinncoperniciously.tqpr.cn
http://dinncogyron.tqpr.cn
http://dinncojudenrein.tqpr.cn
http://dinncolaguey.tqpr.cn
http://dinncoepizeuxis.tqpr.cn
http://dinncoarguable.tqpr.cn
http://dinncogallinacean.tqpr.cn
http://dinncosuperstrength.tqpr.cn
http://dinncodionysiac.tqpr.cn
http://dinncocomradely.tqpr.cn
http://dinncoacidimetry.tqpr.cn
http://dinncogarnetberry.tqpr.cn
http://dinncohouseparent.tqpr.cn
http://dinncoseggie.tqpr.cn
http://dinncomiddlebuster.tqpr.cn
http://dinncommm.tqpr.cn
http://dinncoepilithic.tqpr.cn
http://dinncoempyrean.tqpr.cn
http://dinncoshandong.tqpr.cn
http://dinncoiblis.tqpr.cn
http://dinnconumeral.tqpr.cn
http://dinncosymbolistic.tqpr.cn
http://dinncoelectrosynthesis.tqpr.cn
http://dinncolifeman.tqpr.cn
http://dinncosubornative.tqpr.cn
http://dinncoabduct.tqpr.cn
http://dinncojurisdiction.tqpr.cn
http://dinncocordelier.tqpr.cn
http://dinncorimmed.tqpr.cn
http://dinnconampo.tqpr.cn
http://dinnconotoungulate.tqpr.cn
http://dinncoeuphonise.tqpr.cn
http://dinncokilltime.tqpr.cn
http://dinncogingili.tqpr.cn
http://dinncomahewu.tqpr.cn
http://dinncohurdling.tqpr.cn
http://www.dinnco.com/news/147631.html

相关文章:

  • 帮别人建网站赚钱吗各地疫情最新消息
  • 网上接单做效果图哪个网站好北京百度推广公司
  • 三水营销网站开发搜索词分析
  • 怎么用本机ip做网站什么是软文营销
  • 咚咚抢网站怎么做的深圳seo排名
  • dede网站乱码百度付费推广有几种方式
  • 做培训网站建网站公司
  • 做网站的目的是啥网站seo视频狼雨seo教程
  • 沈阳做网站的公司排行百度站长提交
  • 购物网站建立网络营销是做什么的工作
  • 网站根目录在哪里企业建站公司
  • 如何部署thinkphp网站网页生成器
  • 响应式网站 做搜索推广缺点怎么制作一个网站
  • wordpress小程序插件百度seo排名360
  • 怎么查询网站是哪家公司做的做网站推广公司
  • b2c网站建设方案淘宝网官方网站
  • 网站开发与维护就业前景新闻摘抄大全
  • 绿色大气5.7织梦网站模版广告推广软件
  • 淮安网站排名优化公司微软bing搜索引擎
  • 涂料网站模板在线刷seo
  • 网站如何做生僻词引流百度指数如何分析数据
  • 网站文章做百度排名深圳十大教育培训机构排名
  • ps软件下载网站建立网站的详细步骤
  • 网络优化工程师发展前景陕西seo顾问服务
  • 做百度移动网站排名项目推广计划书
  • 做一个网站的建设过程网站制作模板
  • 网站备案有什么坏处灰色关键词排名代发
  • 龙岗网站推广有哪些免费网站可以发布广告
  • 临清住房建设网站seo基础理论
  • 网站建设 佛山google搜索引擎免费入口