4 Replies Latest reply on Aug 25, 2020 7:28 PM by FeYu_1508691

    How to stop a running thread?




      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)
      //do something


      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.