OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 6877|回复: 3

rt_thread_yield好像没起作用是为什么

[复制链接]

7

主题

23

帖子

0

精华

初级会员

Rank: 2

积分
80
金钱
80
注册时间
2020-5-9
在线时间
13 小时
发表于 2020-12-28 16:32:21 | 显示全部楼层 |阅读模式
1金钱
int main(void)
{
    delay_init(168);
    MX_USART1_UART_Init();
    freertos_init();
}
static void thread1_entry(void *parameter)
{
    rt_uint32_t count = 0;
    while(1)
    {
        rt_kprintf("thread 1 count = %d\r\n",count++);
        rt_thread_yield();
    }
}

static void thread2_entry(void *parameter)
{
    rt_uint32_t count = 0;
    while(1)
    {
        rt_kprintf("thread 2 count = %d\r\n",count++);
        rt_thread_yield();
    }
}

static void startDefaultTaskFuc(void *parameter)
{
        tid2 = rt_thread_create("t1", thread1_entry, RT_NULL, 512, 5, 5);
        if(tid2!=RT_NULL)
            rt_thread_startup(tid2);
        else
        {
            rt_kprintf("create thread1 failed!\r\n");
        }

        tid3 = rt_thread_create("t2", thread2_entry, RT_NULL, 512, 5, 5);
        if(tid3 !=RT_NULL)
        {
            rt_thread_startup(tid3);
        }
        else
        {
            rt_kprintf("create thread2 failed!\r\n");
        }
}
void freertos_init(void)
{
    rt_thread_init(&startDefaultTask, "start", startDefaultTaskFuc, RT_NULL, rt_startDefaultTask_stack, sizeof(rt_startDefaultTask_stack), 3, 20);
    rt_thread_startup(&startDefaultTask);
}


以上程序,理论上应该是线程1和线程2交替运行吧,为什么我的rt_thread_yield好像不起作用 ,rt_thread_yield函数中该处if (to_thread != rt_current_thread)判断错误。在打印函数还未执行完就跳到另一个线程了
收←◆thread 1 count = 0
thread 1 count = 1
thread 1 count = 2
thread 1 count = 3
thread 1 count = thread 2 count = 0
thread 2 count = 1
thread 2 c2
thread 1 count = 5
thread 1 count = 6
threadount = 7
thread 2 count = 3
thread 2 count = 4
2 count = 4
thread 1 count = 8
thread 1 count =thread 2 count = 5
thread 2 count = 6
thread 2 c 7
thread 1 count = 10
thread 1 count = 11
thrount = 12thread 2 count = 8
thread 2 count = 9
ead 2 count = 9
\0thread 1 count = 13



正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

7

主题

23

帖子

0

精华

初级会员

Rank: 2

积分
80
金钱
80
注册时间
2020-5-9
在线时间
13 小时
 楼主| 发表于 2020-12-28 17:02:26 | 显示全部楼层
本帖最后由 yzxwy 于 2020-12-28 17:07 编辑

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "key.h"
#include "rtthread.h"
/************************************************
正点原子 探索者F407开发板        RT-Thread实验7
yield切换线程 - 库函数版本

RT-Thread官方微信公众号:RTThread
RT-Thread技术论坛:http://www.rt-thread.org

正点原子官方微信公众号:正点原子
正点原子技术论坛:www.openedv.com
正点原子淘宝店铺:http://eboard.taobao.com

作者: RT-Thread        &        正点原子
************************************************/



/* 重映射串口1到rt_kprintf */
void rt_hw_console_output(const char *str)
{        
        rt_enter_critical();

        while (*str!='\0')
        {
                if (*str=='\n')
                {
                        USART_SendData(USART1, '\r');
                        while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
                }

                USART_SendData(USART1, *str++);                                 
                while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);        
        }        

        rt_exit_critical();
}

#define THREAD_STACK_SIZE 512
#define THREAD_PRIORITY   2
#define THREAD_TIMESLICE  20

/* 指向线程控制块的指针 */
static rt_thread_t tid1 = RT_NULL;
static rt_thread_t tid2 = RT_NULL;

/* 线程1入口 */
static void thread1_entry(void* parameter)
{
    rt_uint32_t count = 0;

    while (1)
    {
                /* 打印线程1的输出 */
                rt_kprintf("thread1: count = %d\n", count ++);

                /* 执行yield后应该切换到thread2执行 */
                rt_thread_yield();
    }
}

/* 线程2入口 */
static void thread2_entry(void* parameter)
{
    rt_uint32_t count = 0;

    while (1)
    {
        /* 打印线程2的输出 */
        rt_kprintf("thread2: count = %d\n", count ++);

        /* 执行yield后应该切换到thread1执行 */
        rt_thread_yield();
    }
}

int main()
{
    /* 创建线程1 */
    tid1 = rt_thread_create("thread",
                                                        thread1_entry,
                                                        RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
                                                        THREAD_STACK_SIZE,
                                                        THREAD_PRIORITY,
                                                        THREAD_TIMESLICE);
    if (tid1 != RT_NULL)
        rt_thread_startup(tid1);

    /* 创建线程2 */
    tid2 = rt_thread_create("thread",
                                                        thread2_entry,
                                                        RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
                                                        THREAD_STACK_SIZE,
                                                        THREAD_PRIORITY,
                                                        THREAD_TIMESLICE);
    if (tid2 != RT_NULL)
        rt_thread_startup(tid2);

    return 0;
}


这是正点原子的例程,优先级太高的话线程2还未创建成功线程1就开始运行,并且main函数无法再获取时间片,将优先级从5改为25的结果有点混乱,但是应该是理想的结果吧。那我改的程序问题出在哪呢?收←◆msh >thread1: count = 0
thread1: count = 1
thrthread2: count = 0
thread2: count = 1
thread2: counead2: count = 2
thread1: count = 3
thread1: count t = 4
thread2: count = 3
thread2: count = 4
threa= 5
thread1: count = 5
thread1: count = 6
thread1d1: count = 7
thread2: count = 6
thread2: count = : count = 7
thread1: count = 8
thread1: count = 99
thread2: count = 8
thread2: count = 9
thread2: 0thread1: count = 10
thread1: count = 11
thread1: ccount = 12
thread2: count = 11
thread2: count = 12ount = 12
thread1: count = 13
thread1: count = 14
thread2: count = 13
thread2: count = 14


回复

使用道具 举报

558

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
164897
金钱
164897
注册时间
2010-12-1
在线时间
2100 小时
发表于 2020-12-29 01:30:48 | 显示全部楼层
帮顶
回复

使用道具 举报

5

主题

269

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1050
金钱
1050
注册时间
2020-5-11
在线时间
252 小时
发表于 2020-12-29 08:58:30 | 显示全部楼层
帮顶                                    
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-6-9 23:02

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表