<?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>Why there is no main() in zephyr thread sample codes?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/59838/why-there-is-no-main-in-zephyr-thread-sample-codes</link><description>Hello: 
 I am studying thread samples in nRF9160. 
 There are two thread samples: &amp;quot;zephyr\samples\basic\threads&amp;quot; and “zephyr\samples\synchronization”. 
 But I can&amp;#39;t find main() in those two samples. 
 So I don&amp;#39;t know where the code started from and how</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 01 Apr 2020 02:00:14 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/59838/why-there-is-no-main-in-zephyr-thread-sample-codes" /><item><title>RE: Why there is no main() in zephyr thread sample codes?</title><link>https://devzone.nordicsemi.com/thread/243288?ContentTypeID=1</link><pubDate>Wed, 01 Apr 2020 02:00:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ee175a54-958a-472d-a4d7-96140912246d</guid><dc:creator>bbslhb</dc:creator><description>&lt;p&gt;Thank you, Simon.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Why there is no main() in zephyr thread sample codes?</title><link>https://devzone.nordicsemi.com/thread/243287?ContentTypeID=1</link><pubDate>Tue, 31 Mar 2020 20:47:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:692b5b9d-c60c-4d60-9851-268d2ea1fca8</guid><dc:creator>Simon</dc:creator><description>[quote user=""]&amp;nbsp; &amp;nbsp; So I don&amp;#39;t know where the code started from[/quote]
&lt;p&gt;In a Zephyr/NCS example&amp;nbsp;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;code&gt;&lt;a href="https://github.com/NordicPlayground/fw-nrfconnect-zephyr/blob/v2.1.99-ncs1/kernel/init.c#L491"&gt;z_cstart()&lt;/a&gt;&amp;rarr;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v2.1.99-ncs1/kernel/init.c#L407"&gt;switch_to_main_thread()&lt;/a&gt;&amp;nbsp;&lt;/code&gt;will run initially, which will invoke&amp;nbsp;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v2.1.99-ncs1/kernel/init.c#L226"&gt;bg_thread_main()&lt;/a&gt;&lt;span&gt;&amp;nbsp; that will look for a&amp;nbsp;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v2.1.99-ncs1/kernel/init.c#L283"&gt;main()&lt;/a&gt;&amp;nbsp;(Which will run in the&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/kernel/threads/system_threads.html#system-threads"&gt;&amp;nbsp;Main System Thread&lt;/a&gt;). If main() is not defined,&amp;nbsp;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.2.0/zephyr/reference/kernel/threads/system_threads.html#system-threads"&gt;there is no problem&lt;/a&gt;&amp;nbsp;(unless&amp;nbsp;&lt;a href="https://docs.zephyrproject.org/latest/reference/kconfig/CONFIG_MULTITHREADING.html"&gt;CONFIG_MULTITHREADING&lt;/a&gt;&amp;nbsp;is set to n):&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;em&gt;&amp;quot;If&amp;nbsp;&lt;code&gt;&lt;span&gt;main()&lt;/span&gt;&lt;/code&gt;&amp;nbsp;is&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;not defined&lt;/strong&gt;, or if it executes and then does a normal return, the main thread terminates normally and no error is raised.&amp;quot;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;If no &lt;em&gt;main()&amp;nbsp;&lt;/em&gt;is defined, &lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v2.1.99-ncs1/kernel/init.c#L294-L297"&gt;this default weak main() function&lt;/a&gt; will be used.&lt;/p&gt;
[quote user=""]and how the code run threads.[/quote]
&lt;p&gt;If you read &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.2.0/zephyr/reference/kernel/threads/index.html#spawning-a-thread"&gt;Threads&amp;rarr;Spawning a Thread&lt;/a&gt;, you can see that a thread can be spawn in two different ways; at run time or at compile time.&amp;nbsp;In the sample &lt;em&gt;zephyr/samples/basic/thread&lt;/em&gt;s&amp;nbsp;the latter method is used (compile-time). Check it out &lt;a href="https://github.com/NordicPlayground/fw-nrfconnect-zephyr/blob/master/samples/basic/threads/src/main.c#L91"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The threads will then be &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/kernel/threads/index.html#thread-states"&gt;ready&lt;/a&gt; and&amp;nbsp;can be executed by the &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/kernel/scheduling/index.html"&gt;scheduler&lt;/a&gt;.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Hopefully, I&amp;#39;ve understood this correctly.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>