How to stop a running thread?

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

cross mob
FeYu_1508691
Level 3
Level 3
First like received

Hello,

I have a question that if I can stop a thread while running?

I have tried to search by Google but found nothing useful.

For example, I have started a thread in CyFxApplicationDefine like:

    retThrdCreate = CyU3PThreadCreate (&cdcThread,         /* UVC Thread structure */

            "32:CDC UARTtoUSB Thread",                          /* Thread Id and name */

            CDCAppThread_Entry,                              /* UVC Application CDC Thread Entry function */

            0,                                                  /* No input parameter to thread */

            ptr3,                                               /* Pointer to the allocated thread stack */

            UVC_APP_CDC_THREAD_STACK,         /* UVC Application Thread stack size */

            UVC_APP_CDC_THREAD_PRIORITY, /* UVC Application Thread priority */

            UVC_APP_CDC_THREAD_PRIORITY, /* Threshold value for thread pre-emption. */

            CYU3P_NO_TIME_SLICE,                                /* No time slice for the application thread */

            CYU3P_AUTO_START                                    /* Start the Thread immediately */

            );

And CDCAppThread_Entry is like:

void CDCAppThread_Entry (uint32_t input)

{

for(;;)

{

//do something

CyU3PThreadSleep(600);

}

}

If I want to stop the thread while running, should I add a flag in the for loop, and check it if true then break the loop?

And the thread will be auto finished after this?

(I have tried this actually, but firmware run freeze. I donot know why.)

Or I should just leave the loop to run freely?

(I have tried this also, everything seems work fine, but I think it is a waste of time to run this thread.)

I am not familiar with the ThreadX RTOS, and do not know what is the correct way to do this.

Thanks.

0 Likes
4 Replies
YiZ_31
Moderator
Moderator
Moderator
1000 replies posted 750 replies posted 500 replies posted

Hello,

ThreadX supports Mutex or Semaphore for threads to signal each other. It is a common way used in RTOS.

You can check demo project RtosExample in FX3SDK for the usage of Semaphore and Mutex.

Regards,
Eddie

0 Likes

Hi Eddie,

Thanks for your reply.

I have checked the rtos example in sdk, but I still do not know how to stop(finish) a thread.

I do not want to do some multi thread operation, I just want to stop an idle thread.

I have found a function called CyU3PThreadDestroy, but do not know how to use it.

Best regards.

0 Likes
YiZ_31
Moderator
Moderator
Moderator
1000 replies posted 750 replies posted 500 replies posted

Hi,

In this thread, you can call CyU3PThreadSuspend to suspend itself. However, you cannot call CyU3PThreadDestroy to destroy itself.

Regards,
Eddie

0 Likes

Hello Eddie,

I have tried CyU3PThreadSuspend to suspend the thread itself today, but the firmware seems freeze.

If I have just leave the thread run loop freely, everything seems go well again.

Best regards.

0 Likes