<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      RT-Thread之創建線程

      一、靜態線程創建

      1、thread_task.c文件

      #include "thread_task.h"
      #include "main.h"
      #include <stdio.h>      
      #include "rtthread.h"
      
      /******************************************** 線程 1 ******************************************************/
      #define THREAD_1_PRIORITY  		5           /* 進程優先級 */
      #define THREAD_1_STACK_SIZE		512         /* 進程棧空間大小 */
      #define THREAD_1_TIMESLICE		1           /* 進程執行時間片個數 */
      static char thread_1_stack[THREAD_1_STACK_SIZE];   /* 進程棧 */
      static struct rt_thread thread_1_handle;        /* 進程句柄 */
      
      /******************************************** 線程 2 ******************************************************/
      #define THREAD_2_PRIORITY  		5           /* 進程優先級 */
      #define THREAD_2_STACK_SIZE		512         /* 進程棧空間大小 */
      #define THREAD_2_TIMESLICE		1           /* 進程執行時間片個數 */
      static char thread_2_stack[THREAD_1_STACK_SIZE];    /* 進程棧 */
      static struct rt_thread thread_2_handle;    /* 進程句柄 */
      
      uint8_t count_1 = 0;
      uint8_t count_2 = 0;
      
      
      /**
       * @brief   線程1入口函數
       * @param   無
       * @retval  無
       */
      void thread_1_entry(void* param)
      {
          while(1)
          {
              rt_tick_t now_tick = rt_tick_get();     /* 獲取當前時間 */
              
              HAL_GPIO_TogglePin(GPIOC, LED1_Pin);
              
      
              rt_thread_delay_until(&now_tick, 1000);     /* 精準延時1000時間片 */
          }
      }
      
      /**
       * @brief   線程2入口函數
       * @param   無
       * @retval  無
       */
      void thread_2_entry(void* param)
      {
          while(1)
          {
              rt_tick_t now_tick = rt_tick_get();     /* 獲取當前時間 */
              
      		HAL_GPIO_TogglePin(GPIOC, LED2_Pin);
              
              rt_thread_delay_until(&now_tick, 1000);     /* 精準延時1000時間片 */
          }
          
      }
      
      /**
       * @brief   創建線程任務并啟動
       * @param   無
       * @retval  無
       */
      void ThreadStart(void)
      {
      	/* 初始化靜態線程*/
      	rt_thread_init(
      		&thread_1_handle,		/* 線程句柄*/
      		"thread_1",				/* 線程句柄名稱*/
      		thread_1_entry,			/* 函數入口 */
      		RT_NULL,				/* 入口函數參數 */
      		&thread_1_stack[0],		/* 線程棧地址 */
      		THREAD_1_STACK_SIZE,	/* 線程棧大小 */
      		THREAD_1_PRIORITY,		/* 線程優先級 */
      		THREAD_1_TIMESLICE);	/* 線程時間片大小 */
      		
      	rt_thread_startup(&thread_1_handle); /* 啟動線程 */
      
      	/* 初始化靜態線程*/
      	rt_thread_init(
      		&thread_2_handle,		/* 線程句柄*/
      		"thread_2",				/* 線程句柄名稱*/
      		thread_2_entry,			/* 函數入口 */
      		RT_NULL,				/* 入口函數參數 */
      		&thread_2_stack[0],		/* 線程棧地址 */
      		THREAD_2_STACK_SIZE,	/* 線程棧大小 */
      		THREAD_2_PRIORITY,		/* 線程優先級 */
      		THREAD_2_TIMESLICE);	/* 線程時間片大小 */
      		
      	rt_thread_startup(&thread_2_handle); /* 啟動線程 */
      }
      
      

      2、thread_task.h文件

      #ifndef __THREAD_TASK_H
      #define __THREAD_TASK_H
      
      #ifdef __cplusplus
      extern "C" {
      #endif
      
      void ThreadStart(void);
      
      #ifdef __cplusplus
      }
      #endif
      
      #endif /* __THREAD_TASK_H */
      
      

      二、靜態創建線程(自定義實現調度)

      1、thread_task.c文件

      #include "thread_task.h"
      #include "main.h"
      #include <stdio.h>      
      #include "rtthread.h"
      
      /******************************************** 線程 1 ******************************************************/
      #define THREAD_1_CYCLE          1000        /* 進程執行周期 */
      #define THREAD_1_PRIORITY  		5           /* 進程優先級 */
      #define THREAD_1_STACK_SIZE		512         /* 進程棧空間大小 */
      #define THREAD_1_TIMESLICE		1           /* 進程執行時間片個數 */
      static char thread_1_stack[THREAD_1_STACK_SIZE];   /* 進程棧 */
      static struct rt_thread thread_1_handle;        /* 進程句柄 */
      
      /******************************************** 線程 2 ******************************************************/
      #define THREAD_2_CYCLE          1000        /* 進程執行周期 */
      #define THREAD_2_PRIORITY  		5           /* 進程優先級 */
      #define THREAD_2_STACK_SIZE		512         /* 進程棧空間大小 */
      #define THREAD_2_TIMESLICE		1           /* 進程執行時間片個數 */
      static char thread_2_stack[THREAD_1_STACK_SIZE];    /* 進程棧 */
      static struct rt_thread thread_2_handle;    /* 進程句柄 */
      
      uint8_t count_1 = 0;
      uint8_t count_2 = 0;
      
      /**
       * @brief   線程1入口函數
       * @param   無
       * @retval  無
       */
      void thread_1_entry(void* param)
      {
          while(1)
          {
              HAL_GPIO_TogglePin(GPIOC, LED1_Pin);
      
              rt_thread_suspend(&thread_1_handle);  //掛起線程
      		rt_schedule();              //啟動調度器
          }
      }
      
      /**
       * @brief   線程2入口函數
       * @param   無
       * @retval  無
       */
      void thread_2_entry(void* param)
      {
          while(1)
          {
      		HAL_GPIO_TogglePin(GPIOC, LED2_Pin);
              
              rt_thread_suspend(&thread_2_handle);  //掛起線程
      		rt_schedule();              //啟動調度器
          }
          
      }
      
      /**
       * @brief   創建線程任務并啟動
       * @param   無
       * @retval  無
       */
      void ThreadStart(void)
      {
      	/* 初始化靜態線程*/
      	rt_thread_init(
      		&thread_1_handle,		/* 線程句柄*/
      		"thread_1",				/* 線程句柄名稱*/
      		thread_1_entry,			/* 函數入口 */
      		RT_NULL,				/* 入口函數參數 */
      		&thread_1_stack[0],		/* 線程棧其實地址 */
      		THREAD_1_STACK_SIZE,	/* 線程棧大小 */
      		THREAD_1_PRIORITY,		/* 線程優先級 */
      		THREAD_1_TIMESLICE);	/* 線程時間片大小 */
      		
      	rt_thread_startup(&thread_1_handle); /* 啟動線程 */
      
      	/* 初始化靜態線程*/
      	rt_thread_init(
      		&thread_2_handle,		/* 線程句柄*/
      		"thread_2",				/* 線程句柄名稱*/
      		thread_2_entry,			/* 函數入口 */
      		RT_NULL,				/* 入口函數參數 */
      		&thread_2_stack[0],		/* 線程棧其實地址 */
      		THREAD_2_STACK_SIZE,	/* 線程棧大小 */
      		THREAD_2_PRIORITY,		/* 線程優先級 */
      		THREAD_2_TIMESLICE);	/* 線程時間片大小 */
      		
      	rt_thread_startup(&thread_2_handle); /* 啟動線程 */
      }
      
      /*任務調度*/
      void Task_Schedule(void)
      {
      	static uint32_t task_count = 0;
      
      	if(task_count%1000 == 0)   rt_thread_resume(&thread_1_handle);
      	if(task_count%2000 == 0)   rt_thread_resume(&thread_2_handle);
      			
      	rt_schedule();
      	task_count++;
          //計數值達到一定值后清零,防止任務調度錯亂
      	if(task_count == 200000)	task_count = 0; 
      }
      
      

      2、thread_task.h文件

      #ifndef __THREAD_TASK_H
      #define __THREAD_TASK_H
      
      #ifdef __cplusplus
      extern "C" {
      #endif
      
      void ThreadStart(void);
      void Task_Schedule(void);
      
      #ifdef __cplusplus
      }
      #endif
      
      #endif /* __THREAD_TASK_H */
      

      2、修改board.c中的SysTick_Handler()函數

      extern void Task_Schedule(void);
      
      void SysTick_Handler(void)
      {
          /* 進入中斷 */
          rt_interrupt_enter();
      
          /* 更新時基 */
          rt_tick_increase();
          /* 自定義調度函數 */
          Task_Schedule();
          
          /* 離開中斷 */
          rt_interrupt_leave();
      }
      

      三、動態創建線程

      1、thread_task.c文件

      #include "thread_task.h"
      #include "main.h"
      #include <stdio.h>      
      #include "rtthread.h"
      
      /******************************************** 線程 1 ******************************************************/
      #define THREAD_1_PRIORITY  		5           /* 進程優先級 */
      #define THREAD_1_STACK_SIZE		512         /* 進程棧空間大小 */
      #define THREAD_1_TIMESLICE		1           /* 進程執行時間片個數 */
      static struct rt_thread *thread_1_handle;        /* 進程句柄 */
      
      /******************************************** 線程 2 ******************************************************/
      #define THREAD_2_PRIORITY  		5           /* 進程優先級 */
      #define THREAD_2_STACK_SIZE		512         /* 進程棧空間大小 */
      #define THREAD_2_TIMESLICE		1           /* 進程執行時間片個數 */
      static struct rt_thread *thread_2_handle;    /* 進程句柄 */
      
      
      
      /**
       * @brief   線程1入口函數
       * @param   無
       * @retval  無
       */
      void thread_1_entry(void* param)
      {
          while(1)
          {
              rt_tick_t now_tick = rt_tick_get();     /* 獲取當前時間 */
              
              HAL_GPIO_TogglePin(GPIOC, LED1_Pin);
      
              rt_thread_delay_until(&now_tick, 1000);     /* 精準延時1000時間片 */
          }
      }
      
      /**
       * @brief   線程2入口函數
       * @param   無
       * @retval  無
       */
      void thread_2_entry(void* param)
      {
          while(1)
          {
              rt_tick_t now_tick = rt_tick_get();     /* 獲取當前時間 */
              
      		HAL_GPIO_TogglePin(GPIOC, LED2_Pin);
              
              rt_thread_delay_until(&now_tick, 1000);     /* 精準延時1000時間片 */
          }
          
      }
      
      /**
       * @brief   動態創建線程任務并啟動
       * @param   無
       * @retval  無
       */
      void ThreadStart(void)
      {
          /* 動態創建線程 */
          thread_1_handle = rt_thread_create(
              "thread_1",				/* 線程句柄名稱*/
              thread_1_entry,			/* 函數入口 */
              RT_NULL,				/* 入口函數參數 */
              THREAD_1_STACK_SIZE,	/* 線程棧大小 */
              THREAD_1_PRIORITY,		/* 線程優先級 */
              THREAD_1_TIMESLICE  	/* 線程時間片大小 */
          );
      
      		
      	rt_thread_startup(thread_1_handle); /* 啟動線程 */
      
          thread_2_handle = rt_thread_create(
                  "thread_2",				/* 線程句柄名稱*/
                  thread_2_entry,			/* 函數入口 */
                  RT_NULL,				/* 入口函數參數 */
                  THREAD_2_STACK_SIZE,	/* 線程棧大小 */
                  THREAD_2_PRIORITY,		/* 線程優先級 */
                  THREAD_2_TIMESLICE  	/* 線程時間片大小 */
              );
      		
      	rt_thread_startup(thread_2_handle); /* 啟動線程 */
      }
      
      

      2、thread_task.h文件

      #ifndef __THREAD_TASK_H
      #define __THREAD_TASK_H
      
      #ifdef __cplusplus
      extern "C" {
      #endif
      
      void ThreadStart(void);
      void Task_Schedule(void);
      
      #ifdef __cplusplus
      }
      #endif
      
      #endif /* __THREAD_TASK_H */
      

      歡迎關注個人微信公眾號:比特向陽;
      您也可以通過郵箱 lsk0358@163.com與我進行交流;

      posted @ 2025-10-27 00:10  比特向陽  閱讀(8)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 亚洲欧美日韩在线码| 日本一区二区三区专线 | 亚洲av噜噜一区二区| 日本一道高清一区二区三区| 九九成人免费视频| 日韩亚洲国产激情一区二区| 亚洲乱女色熟一区二区三区| 亚洲精品麻豆一二三区| 日韩中文字幕人妻一区| 无码专区 人妻系列 在线| 国产亚洲精品自在久久蜜TV| 成人3D动漫一区二区三区| 动漫精品中文无码卡通动漫| 国产美女被遭强高潮免费一视频| 久久午夜夜伦鲁鲁片免费无码| 国产精品亚洲аv无码播放| 成人国产精品三上悠亚久久| 97久久久亚洲综合久久| 国产AV国片精品有毛| 亚洲精品一区二区三区片| 精品国产成人a在线观看| 9久9久热精品视频在线观看| 日韩激情一区二区三区| 国产视频一区二区| 日韩成人午夜精品久久高潮| 国产普通话刺激视频在线播放| 柳林县| 狠狠色噜噜狠狠狠888米奇视频| 91精品国产午夜福利| 国产精久久一区二区三区| 国产精品中文字幕第一区| 欧洲美熟女乱av在免费| 无码伊人久久大杳蕉中文无码 | 亚洲欧美综合精品成| 国产精品无码午夜福利| 日韩成人高精品一区二区| 欧美性白人极品hd| 国产成人综合亚洲精品国产| 国产精品免费看久久久| 日韩精品亚洲专在线电影| 蜜桃av亚洲精品一区二区|