How can I terminate a thread that is in a waiting state?

Here it is suggested to gracefully terminate a thread by sending it a notification of any kind. But how can I terminate a thread that is in a waiting state with K_FOREVER and that I know will never unblock, without using k_thread_abort()? An example of a thread to terminate could be the following:

void Foo_Thread( void )
{
    fifo_ble_communication_item_t *fifoItem;

    for ( ;; )
    {
        fifoItem = k_fifo_get( &fooFifo, K_FOREVER );
        
        /* use fifoItem data */
    }
}

Parents
  • Hmm, 

    Something is not right, 

    But how can I terminate a thread that is in a waiting state with K_FOREVER and that I know will never unblock

    A thread that have reached to this point seems like a mistake and should have aborted if it went into that state knowingly. If that thread went into this state due to unhandled conditions, and another thread can detect this, then there are many ways to do this. Use flags or timeouts 

    For example using a flag like terminate_thread_flag

    static volatile bool terminate_thread_flag = false;
    
    void main(void)
    {
        k_tid_t thread_id = k_thread_create(..., Foo_Thread, ...);
    
        // wome work
        xxx_xxx
    
        // Notify the thread to terminate
        terminate_thread_flag = true;
    
    
    }

    and the thread that needs killing can have the flag checked in the start like below

    void Foo_Thread(void)
    {
        struct fifo_item *fifoItem;
    
        while (!terminate_thread_flag) {
        ...
        ...

Reply
  • Hmm, 

    Something is not right, 

    But how can I terminate a thread that is in a waiting state with K_FOREVER and that I know will never unblock

    A thread that have reached to this point seems like a mistake and should have aborted if it went into that state knowingly. If that thread went into this state due to unhandled conditions, and another thread can detect this, then there are many ways to do this. Use flags or timeouts 

    For example using a flag like terminate_thread_flag

    static volatile bool terminate_thread_flag = false;
    
    void main(void)
    {
        k_tid_t thread_id = k_thread_create(..., Foo_Thread, ...);
    
        // wome work
        xxx_xxx
    
        // Notify the thread to terminate
        terminate_thread_flag = true;
    
    
    }

    and the thread that needs killing can have the flag checked in the start like below

    void Foo_Thread(void)
    {
        struct fifo_item *fifoItem;
    
        while (!terminate_thread_flag) {
        ...
        ...

Children
Related