<?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>Best practice for spawning a (short) Zephyr thread from main and terminating it?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/111532/best-practice-for-spawning-a-short-zephyr-thread-from-main-and-terminating-it</link><description>Hi, I am relatively new to the Nordic platform and Zephyr RTOS. I am using nRF Connect SDK version 2.6.0 (the VS Code extension on Windows 11) with the nRF52840DK. I was wondering what the best strategy would be for spawning threads that do a small task</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 31 May 2024 14:26:28 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/111532/best-practice-for-spawning-a-short-zephyr-thread-from-main-and-terminating-it" /><item><title>RE: Best practice for spawning a (short) Zephyr thread from main and terminating it?</title><link>https://devzone.nordicsemi.com/thread/486939?ContentTypeID=1</link><pubDate>Fri, 31 May 2024 14:26:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fe8d572a-5a26-4f49-b84f-ec65ebf3746d</guid><dc:creator>nik2k</dc:creator><description>&lt;p&gt;Thanks. My app will actually be using interrupts for handling double and long presses (using Zephyr timers). Once the presses are detected, GPIO pulses are ignored and the vibration-motor-thread is spawned. So the case that you mentioned has been covered.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Best practice for spawning a (short) Zephyr thread from main and terminating it?</title><link>https://devzone.nordicsemi.com/thread/486767?ContentTypeID=1</link><pubDate>Fri, 31 May 2024 05:15:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:59747db6-361b-4ba4-8298-563ba701495e</guid><dc:creator>Susheel Nuguru</dc:creator><description>[quote user="nik2k"]I&amp;#39;m a little confused, sorry. I&amp;#39;m under the impression that the thread will be spawned &lt;strong&gt;only&lt;/strong&gt; when &lt;strong&gt;k_create_thread()&amp;nbsp;&lt;/strong&gt;is called, which is enclosed within the conditional&amp;nbsp;&lt;strong&gt;if (val == 1)&lt;/strong&gt;.&amp;nbsp;[/quote]
&lt;p&gt;My bad, yes, you have covered that.&lt;/p&gt;
&lt;p&gt;Also, one small observation. It looks like you have to consider that the user might do a long button press longer than 100ms in which case the thread will be created, run and aborted more than once for one long button press. Just saying that you might need to calibrate the k_sleep time not just for spawned thread to exit but also for long button presses.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Best practice for spawning a (short) Zephyr thread from main and terminating it?</title><link>https://devzone.nordicsemi.com/thread/486735?ContentTypeID=1</link><pubDate>Thu, 30 May 2024 17:21:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:550e78eb-91c0-4226-a059-fdf8f6442959</guid><dc:creator>nik2k</dc:creator><description>[quote userid="6207" url="~/f/nordic-q-a/111532/best-practice-for-spawning-a-short-zephyr-thread-from-main-and-terminating-it/486005"] seems to be a good approach to create a thread in main like you have done. It looks like the thread is created in main without any condition, so when main enters, the thread for button_pressed is created,[/quote]
&lt;p&gt;I&amp;#39;m a little confused, sorry. I&amp;#39;m under the impression that the thread will be spawned &lt;strong&gt;only&lt;/strong&gt; when &lt;strong&gt;k_create_thread()&amp;nbsp;&lt;/strong&gt;is called, which is enclosed within the conditional&amp;nbsp;&lt;strong&gt;if (val == 1)&lt;/strong&gt;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In my case, I am terminating without any condition and there&amp;#39;s no cleanup since I don&amp;#39;t have any mutexes, kernel objects or malloc-allocated memory. So I think I&amp;#39;m good on that part, right?&lt;/p&gt;
&lt;p&gt;Actually I am going to be adapting this code for my application in which a newly spawned thread will pulse a GPIO pin connected to a vibration motor (for a haptic feedback), on button press. So the same application logic as here, except &lt;strong&gt;button_pressed()&lt;/strong&gt; will have some &lt;strong&gt;gpio_pin_set_dt()&lt;/strong&gt;&amp;nbsp; and &lt;strong&gt;k_msleep()&lt;/strong&gt; calls. Do you have any suggestions on what priority I could assign to such a thread? I know that the main thread has priority 0. For now, I&amp;#39;m using priority 5.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Best practice for spawning a (short) Zephyr thread from main and terminating it?</title><link>https://devzone.nordicsemi.com/thread/486005?ContentTypeID=1</link><pubDate>Mon, 27 May 2024 10:52:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ace95122-4ec5-4603-92e7-b1f12ef428ef</guid><dc:creator>Susheel Nuguru</dc:creator><description>[quote user=""]1. is this the correct approach for spawning short-lived threads? Am I forgetting to do some cleanup? and are there better approaches to use cases such as this ?[/quote]
&lt;p&gt;Creation : seems to be a good approach to create a thread in main like you have done. It looks like the thread is created in main without any condition, so when main enters, the thread for button_pressed is created,&lt;/p&gt;
&lt;p&gt;Termination :&amp;nbsp; Your thread button_pressed needs to wait for something or sleep until the condition for the thread termination.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;In Zephyr, a thread can terminate its execution in two ways:&lt;/p&gt;
&lt;p&gt;1. &lt;a href="https://docs.nordicsemi.com/bundle/ncs-latest/page/zephyr/kernel/services/threads/index.html#thread_termination"&gt;Termination&lt;/a&gt;: A thread can synchronously end its execution by returning from its entry point function. This is known as termination. A thread that terminates is responsible for releasing any shared resources it may own (such as mutexes and dynamically allocated memory) prior to returning, since the kernel does not reclaim them automatically.&lt;/p&gt;
&lt;p&gt;Here is an example of how a thread can terminate:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void my_entry_point(int unused1, int unused2, int unused3)
{
while (1) {
...
if (&amp;lt;some condition&amp;gt;) {
return; /* thread terminates from mid-entry point function */
}
...
}

/* thread terminates at end of entry point function */
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;2. &lt;strong&gt;Aborting&lt;/strong&gt;: A thread may asynchronously end its execution by aborting. The kernel automatically aborts a thread if the thread triggers a fatal error condition, such as dereferencing a null pointer. A thread can also be aborted by another thread (or by itself) by calling `k_thread_abort()`. However, it is typically preferable to signal a thread to terminate itself gracefully, rather than aborting it.&lt;/p&gt;
&lt;p&gt;Here is an example of how a thread can be aborted:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;k_thread_abort(thread_id);
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;In both cases, once a thread has terminated, the kernel guarantees that no use will be made of the thread struct. The memory of such a struct can then be re-used for any purpose, including spawning a new thread.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>