questions about time delay in the while(1) block of a task

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

I tested the time delay functions in the while(1) block of a task. But I found some strange results that are hard to understand. The test are presented as follows:

Environment : WICED6.1+CYW94307+ThreadX

I created two tasks,i.e. task1 and task2, with the same or different priorities. The code of each task is as follows:

void thread1_main( uint32_t arg )

{

    uint32_t temp = arg ;

    while ( 1 )

    {

        printf( "This thread 111111\r\n" ) ;

        wiced_rtos_delay_milliseconds( 1 ) ;

    }

    WICED_END_OF_CURRENT_THREAD( ) ;

}

The results are pasted here:

priority set

delay time(in ms)

result

task1 : 7 , task2 : 7

1 or longer

both tasks can run correctly

task1 : 7 , task2 : 8

1

only task1 can run correctly

task1 : 7 , task2 : 8

2 or longer

both tasks can run correctly

                                     

So, my first question is:

1、When the priorities are set different, why only task1 run correctly when the delay time is 1ms , but both tasks run when the delay time is longer?

Another question is:

2、When I replace wiced_rtos_delay_milliseconds(1) with wiced_rtos_delay_microseconds(1000) or even wiced_rtos_delay_microseconds(1000000) , only the task with higher priority run. Why does not the wiced_rtos_delay_microseconds() function cause task scheduling?

0 Likes
1 Solution
ShengY_96
Employee
Employee
10 sign-ins 5 sign-ins Welcome!

Q1、When the priorities are set different, why only task1 run correctly when the delay time is 1ms , but both tasks run when the delay time is longer?

A: Seems the reason is: The execute time of code: printf( "This thread 111111\r\n" ) ; Plus the thread switch time is longer than 1ms and less than 2ms. So the situation: task1: 7, task2:8, delay(1ms)'s switch logic is: Switch to Task1->Printf("This thread 1111111\r\n")-> Suspend Task1-1ms->Switch to Task 2->Printf("This thread 22222\r\n"); but this is not execute completely(Like only print "T")as the Task1 is triggered after 1ms-> Switch to Task 1-> Printf("This thread 1111111\r\n"). Try to use this printf("1\n\r"); or larger the delay time.

Q2. When I replace wiced_rtos_delay_milliseconds(1) with wiced_rtos_delay_microseconds(1000) or even wiced_rtos_delay_microseconds(1000000) , only the task with higher priority run. Why does not the wiced_rtos_delay_microseconds() function cause task scheduling?

A: wiced_rtos_delay_miliseconds include the tx_thread_sleep which can set the task to suspend but wiced_rtos_delay_microseconds had not include the tx_thread_sleep. Say: the wiced_rtos_delay_microseconds only include the delay function not include the RTOS suspend function.

Regards

Jenson

View solution in original post

1 Reply
ShengY_96
Employee
Employee
10 sign-ins 5 sign-ins Welcome!

Q1、When the priorities are set different, why only task1 run correctly when the delay time is 1ms , but both tasks run when the delay time is longer?

A: Seems the reason is: The execute time of code: printf( "This thread 111111\r\n" ) ; Plus the thread switch time is longer than 1ms and less than 2ms. So the situation: task1: 7, task2:8, delay(1ms)'s switch logic is: Switch to Task1->Printf("This thread 1111111\r\n")-> Suspend Task1-1ms->Switch to Task 2->Printf("This thread 22222\r\n"); but this is not execute completely(Like only print "T")as the Task1 is triggered after 1ms-> Switch to Task 1-> Printf("This thread 1111111\r\n"). Try to use this printf("1\n\r"); or larger the delay time.

Q2. When I replace wiced_rtos_delay_milliseconds(1) with wiced_rtos_delay_microseconds(1000) or even wiced_rtos_delay_microseconds(1000000) , only the task with higher priority run. Why does not the wiced_rtos_delay_microseconds() function cause task scheduling?

A: wiced_rtos_delay_miliseconds include the tx_thread_sleep which can set the task to suspend but wiced_rtos_delay_microseconds had not include the tx_thread_sleep. Say: the wiced_rtos_delay_microseconds only include the delay function not include the RTOS suspend function.

Regards

Jenson