This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TSK_yield and TSK_sleep()

Expert 1005 points

Hello,

  I have the following code:

 

#include <std.h>

#include<stdio.h>

#include <log.h>

#include "TestTaskcfg.h"

 

int T1_Value=0;

int T2_Value=0;

 

 

void main()

{

                printf("Program started\n");

 

}

 

 

 

Void Task_1(Arg id_arg) // send some data to task 2

{

                int i;

                for(i=0;;i++)

                {

               

                                printf("Value sent by Task 1- %d\n",i);

                                T1_Value=i*2;

                                SEM_postBinary(&T1_W); // tell that value is updated

                                SEM_pendBinary(&T1_R, SYS_FOREVER); // wait until it is read by kernel

                }

 

}

 

Void Task_2(Arg id_arg) // get data from task1 and print it.

{

 

                int i;

                for(i=0;;i++)

                {

                                SEM_postBinary(&T2_R); // tell the kernel that you are ready to read value

                                SEM_pendBinary(&T2_W, SYS_FOREVER); // wait until value become ready

                                printf("value read In Task 2- %d\n",i);

                                printf("Value= %d\n",T2_Value);

                }

}

 

Void Task_Kernel(Arg id_arg)

{

                for(;;)

                {                             

int T1_W_Count=SEM_count(&T1_W);

                                int T2_R_Count=SEM_count(&T2_R);

 

                                if(T1_W_Count==1 && T2_R_Count==1)

                                {

 

                                                T2_Value=T1_Value;

                                                SEM_reset(&T1_W,0);

                                                SEM_reset(&T2_R,0);   

                                                SEM_post(&T2_W);

                                                SEM_post(&T1_R);

 

                                }

                                TSK_sleep(1);

//                            TSK_yield();

                }

}

It consists of 3 tasks.

The priority for task_1 and task_2 are 1 and for task_kernal it is 2.

 

If I run this code, I am getting this output:

 

Program started

Value sent by Task 1- 0

value read In Task 2- 0

Value= 0

Value sent by Task 1- 1

value read In Task 2- 1

Value= 2

Value sent by Task 1- 2

value read In Task 2- 2

Value= 4

Value sent by Task 1- 3

value read In Task 2- 3

Value= 6

Value sent by Task 1- 4

value read In Task 2- 4

Value= 8

 

 

If I comment out TSK_sleep(1) and uncomment TSK_yield(), I am not getting any output (other than program started).

 

So what is the difference between TSK_sleep(1) and TSK_yield()?

I believe using TSK_sleep() is a waste of CPU time, is there any other way to do this without using  TSK_sleep()?

 

Regards

 

  • Straight from the DSP/BIOS API Reference Guide:

    TSK_sleep - Delay execution of the current task
    TSK_yield - Yield processor to equal priority task

    TSK_yield will only work if there's another task at priority level 2. Because the other tasks are both priority 1 then the TSK_yield does not actually halt processing of TSK_kernel.

    TSK_sleep instead completely halts the execution of the TSK_kernel for the number of 'ticks' specified in the argument (usually this is 1ms). When that time is up the task is then ready to execute again.

  • Hi Tim,

     

    Long time no see :-). I came across your post and I am wondering, is there any difference between TSK_sleep(0) and TSK_yield()?