<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How can I terminate a thread that is in a waiting state?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/118048/how-can-i-terminate-a-thread-that-is-in-a-waiting-state</link><description>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</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 16 Jan 2025 09:36:47 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/118048/how-can-i-terminate-a-thread-that-is-in-a-waiting-state" /><item><title>RE: How can I terminate a thread that is in a waiting state?</title><link>https://devzone.nordicsemi.com/thread/518581?ContentTypeID=1</link><pubDate>Thu, 16 Jan 2025 09:36:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:762e0005-9992-4619-84d4-afaf153497c7</guid><dc:creator>Andrea Verdecchia</dc:creator><description>&lt;p&gt;Thank you for clarifying&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How can I terminate a thread that is in a waiting state?</title><link>https://devzone.nordicsemi.com/thread/518571?ContentTypeID=1</link><pubDate>Thu, 16 Jan 2025 09:11:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:37840afb-4d4b-4eb1-8cd8-9f90b38d7e5d</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;You are right Andrea, the else if statement takes care of that. So flag seems to be a good choice ...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How can I terminate a thread that is in a waiting state?</title><link>https://devzone.nordicsemi.com/thread/518508?ContentTypeID=1</link><pubDate>Wed, 15 Jan 2025 18:54:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cfe0d890-df51-4f24-8e1d-3f7769067267</guid><dc:creator>Andrea Verdecchia</dc:creator><description>[quote userid="6207" url="~/f/nordic-q-a/118048/how-can-i-terminate-a-thread-that-is-in-a-waiting-state/518505"]getting a fifo item within 100 milliseconds else this thread will be killed[/quote]
&lt;p&gt;Why do you say that if I don&amp;#39;t receive a FIFO object, the process gets killed? In my last example, if the timeout expires, the FIFO object will be NULL, and in that case, if the variable &lt;code&gt;terminate_thread_flag&lt;/code&gt; has been set, then I terminate the process with a return. Otherwise, I restart the &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How can I terminate a thread that is in a waiting state?</title><link>https://devzone.nordicsemi.com/thread/518505?ContentTypeID=1</link><pubDate>Wed, 15 Jan 2025 17:35:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7b33c7f4-fd7f-496f-a6d1-10fa48edf2e1</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;The concern with using timeout in fifo_get is that you count on getting a fifo item within 100 milliseconds else this thread will be killed. If that fits your design, that a delay of 100ms in getting a fifo item means you wont get it anymore then the above code looks right. Else I would use abort.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How can I terminate a thread that is in a waiting state?</title><link>https://devzone.nordicsemi.com/thread/518489?ContentTypeID=1</link><pubDate>Wed, 15 Jan 2025 16:44:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:47e231c7-a02c-4b3d-a12d-c5179b32fad5</guid><dc:creator>Andrea Verdecchia</dc:creator><description>&lt;p&gt;The type of thread I used as an example remains indefinitely waiting for incoming data (in this case, via a FIFO). To terminate such a thread that is waiting for data that will no longer arrive, is it preferable to use a timeout like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void Foo_Thread(void) {
    fifo_ble_communication_item_t *fifoItem;

    for (;;) {
        fifoItem = k_fifo_get(&amp;amp;fooFifo, K_MSEC(100));
        
        if (fifoItem != NULL) {
            /* use fifoItem data */
        } else if (terminate_thread_flag == true) {
            return;
        }
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Or k_thread_abort() without the timeout?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How can I terminate a thread that is in a waiting state?</title><link>https://devzone.nordicsemi.com/thread/518480?ContentTypeID=1</link><pubDate>Wed, 15 Jan 2025 15:50:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:777f6e07-4f19-4647-b81f-5d8f96a06071</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Hmm,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Something is not right,&amp;nbsp;&lt;/p&gt;
[quote user=""]But how can I terminate a thread that is in a waiting state with &lt;code&gt;K_FOREVER&lt;/code&gt; and that I know will never unblock[/quote]
&lt;p&gt;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&amp;nbsp;&lt;/p&gt;
&lt;p&gt;For example using a flag like terminate_thread_flag&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;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;


}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;and the thread that needs killing can have the flag checked in the start like below&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void Foo_Thread(void)
{
    struct fifo_item *fifoItem;

    while (!terminate_thread_flag) {
    ...
    ...&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>