<?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>Socket manager</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/65943/socket-manager</link><description>Hello, 
 Multiple threads on our board require network connections, which can be over either the LTE-M modem, or over ethernet. 
 To this end, we created a Socket Manager thread. This receives requests from the other threads and should open and close</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 15 Sep 2020 15:01:36 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/65943/socket-manager" /><item><title>RE: Socket manager</title><link>https://devzone.nordicsemi.com/thread/269717?ContentTypeID=1</link><pubDate>Tue, 15 Sep 2020 15:01:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:10d4c2da-b05e-40ce-9285-8a613e884b8e</guid><dc:creator>newUser</dc:creator><description>&lt;p&gt;Hi Carl,&lt;/p&gt;
&lt;p&gt;Some quick experimentation proved that I should not always believe the Zephyr documentation:&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;k_thread_stack_t *testStacks[8];
static struct k_thread testThreads[8];

K_THREAD_STACK_DEFINE(networkingThreadStack, 2024);
struct k_thread networkingThread;



static void TestThreadFxn (const uint8_t *pId)
{
  printk(&amp;quot;Created thread %u\n&amp;quot;, *pId);
}

static void NetworkingThreadFxn (void)
{
  for (uint8_t i = 0; i &amp;lt; 8; i++)
  {
    testStacks[i] = k_malloc(1024);
    uint8_t *pId = k_malloc(sizeof(uint8_t));
    *pId = i;

    k_thread_create(&amp;amp;testThreads[i], testStacks[i],
                    1024,
                    (k_thread_entry_t) TestThreadFxn,
                    pId, NULL, NULL,
                    NETWORKING_THREAD_PRIORITY, 0, K_NO_WAIT);
  }
}

void NetworkingCreateThread (void)
{
  k_thread_create(&amp;amp;networkingThread, networkingThreadStack,
                  K_THREAD_STACK_SIZEOF(networkingThreadStack),
                  (k_thread_entry_t) NetworkingThreadFxn,
                  NULL, NULL, NULL,
                  NETWORKING_THREAD_PRIORITY, 0, K_NO_WAIT);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;Created thread 0
Created thread 1
Created thread 2
Created thread 3
Created thread 4
Created thread 5
Created thread 6
Created thread 7&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;No crashes, as long as I allocate enough memory as stack.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Socket manager</title><link>https://devzone.nordicsemi.com/thread/269702?ContentTypeID=1</link><pubDate>Tue, 15 Sep 2020 14:34:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d74eb867-c4db-4db1-9fc2-74680e00c3a8</guid><dc:creator>newUser</dc:creator><description>&lt;p&gt;Hi Carl,&lt;/p&gt;
&lt;p&gt;Thank you for your response, the extra information, and for taking the time to test it. I also saw the page you linked, but for k_object_alloc() it says &amp;quot;Currently, allocation of thread stacks is not supported.&amp;quot;&lt;/p&gt;
&lt;p&gt;Some more googling led me to CONFIG_DYNAMIC_OBJECTS, which I enabled and did not get an error on yet. I&amp;#39;ll let you know if I have any luck.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Socket manager</title><link>https://devzone.nordicsemi.com/thread/269701?ContentTypeID=1</link><pubDate>Tue, 15 Sep 2020 14:30:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:16456c5a-69ce-4155-a762-b6afc081209a</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;Hi!&lt;br /&gt;&lt;br /&gt;That makes sense and of course it&amp;#39;s okay!&amp;nbsp;&lt;br /&gt;&lt;br /&gt;I did some more digging in the documentation and it seems that it may be possible after all. Take a look &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/usermode/kernelobjects.html"&gt;here&lt;/a&gt;. I&amp;#39;m not totally sure yet, though and will test it, but that will probably not be finished before noon tomorrow.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Carl Richard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Socket manager</title><link>https://devzone.nordicsemi.com/thread/269670?ContentTypeID=1</link><pubDate>Tue, 15 Sep 2020 12:57:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2b052d3a-57da-467e-87ce-07460ff15578</guid><dc:creator>newUser</dc:creator><description>&lt;p&gt;Hi again Carl!&lt;/p&gt;
&lt;p&gt;I figured that yesterday was more about the higher level whereas this post was more directed to sockets specifically, hence the new post. I hope that&amp;#39;s okay.&lt;/p&gt;
&lt;p&gt;I was indeed curious whether thread stacks could be allocated dynamically, but if I understand correctly the answer is no. This means I should initialize 8 socket handler thread stacks beforehand (as the socket API supports 8 sockets I believe), even though I might not actually use all 8.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Socket manager</title><link>https://devzone.nordicsemi.com/thread/269668?ContentTypeID=1</link><pubDate>Tue, 15 Sep 2020 12:50:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:80546b3a-e5cb-437f-9e6c-5437a6e99488</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;Hello!&amp;nbsp;&lt;br /&gt;&lt;br /&gt;As mentioned yesterday the event manager should be ideal for this kind of operation. Furthermore, I agree that dedicated threads for the sockets is a good idea. If you check out the &lt;a href="https://docs.zephyrproject.org/latest/reference/kernel/threads/"&gt;Zephyr Documentation on threads&lt;/a&gt;&amp;nbsp;it covers quite thoroughly how they are spawned, terminated and managed, so I recommend reading through that.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;The available stack for threads must be allocated on beforehand, but threads can be spawned and terminated freely from what I know.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Carl Richard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>