3 Replies Latest reply on Aug 29, 2014 10:28 AM by seyhan

    Question about create and terminate threads

    sam.lin

      Test on 3.0.1 sdk with FreeRTOS+LwIP build.

      I hit some issues about wiced_rtos_create_thread:

      I thought if a thread is terminated, I can create a new one.

      A simple test shows wiced_rtos_create_thread() will fail after a few iteration.

       

      static wiced_thread_t       my_thread[20];

      static void test_thread_main(uint32_t arg)

      {

              WPRINT_APP_INFO(( "Thread #%d begin\n", arg ));

              wiced_rtos_delay_milliseconds(1000);

              WPRINT_APP_INFO(( "Thread #%d end\n", arg ));

              WICED_END_OF_CURRENT_THREAD();

      }

       

      void application_start(void)

      {

        // do basic init ....

              for (i =0;i< 20; i++) {

                      wiced_result_t status = wiced_rtos_create_thread(&my_thread[i], WICED_DEFAULT_LIBRARY_PRIORITY, "test thread",

                                                                       test_thread_main, 5000, i);

                      if (status != WICED_SUCCESS)

                              WPRINT_APP_INFO(( "create thread fail, status=%u\n", status ));

       

       

                      wiced_rtos_delay_milliseconds(1000);

              }

      }

       

      Then I got below result:

      Thread #0 begin

      Thread #0 end

      Thread #1 begin

      Thread #1 end

      Thread #2 begin

      Thread #2 end

      Thread #3 begin

      Thread #3 end

      Thread #4 begin

      Thread #4 end

      Thread #5 begin

      Thread #5 end

      Thread #6 begin

      Thread #6 end

      Thread #7 begin

      Thread #7 end

      Thread #8 begin

      Thread #8 end

      Thread #9 begin

      Thread #9 end

      Thread #10 begin

      Thread #10 end

      Thread #11 begin

      Thread #11 end

      Thread #12 begin

      Thread #12 end

      create thread fail, status=1031

      create thread fail, status=1031

      create thread fail, status=1031

      create thread fail, status=1031

      create thread fail, status=1031

      create thread fail, status=1031

      create thread fail, status=1031

      ...

       

      So, why wiced_rtos_create_thread() fails after 12 iterations?

      I thought each thread is terminated then it should be ok to create a new one.

       

      If I remove WICED_END_OF_CURRENT_THREAD(), system hangs after thread #1 end.

      What is the proper way to terminate a thread?

       

      Any limitation about the number of thread can be created? Is this limitation configurable?

       

      Thanks.

        • 1. Re: Question about create and terminate threads
          grga

          Hello,

          Please change to the following and report back.

          Thank you.



          static wiced_thread_t       my_thread[50];

          static void test_thread_main(uint32_t arg)

          {

                WPRINT_APP_INFO(( "Thread #%d begin\n", arg ));

                wiced_rtos_delay_milliseconds(1000);

                WPRINT_APP_INFO(( "Thread #%d end\n", arg ));

                WICED_END_OF_CURRENT_THREAD();

          }

           

          void application_start(void)

          {

           

            int i;

          // do basic init ....

                for (i =0;i< 50; i++) {

                        wiced_result_t status = wiced_rtos_create_thread(&my_thread[i], WICED_DEFAULT_LIBRARY_PRIORITY, "test thread",

                                                                         test_thread_main, 5000, i);

                        if (status != WICED_SUCCESS)

                                WPRINT_APP_INFO(( "create thread fail, status=%u\n", status ));

           

           

                        wiced_rtos_delay_milliseconds(1000);

                        wiced_rtos_thread_join(&my_thread[i]);

                        wiced_rtos_delete_thread(&my_thread[i]);

                }

          }

          dshenoy thanks!

          • 2. Re: Question about create and terminate threads
            sam.lin

            Hi Gangi,

            Thanks for the reply.

            I also found the root cause today.

            I thought WICED_END_OF_CURRENT_THREAD(); will kill the thread.

            Turns out it  also needs a "wiced_rtos_delete_thread(&my_thread[i]);" call to make things work.

             

            Thanks.

            • 3. Re: Question about create and terminate threads
              seyhan

              Hi,

               

              Systems like Wiced is targeting threads are created and work is assigned to them as needed. If the threads are terminated some other thread needs to do the cleanup. Perhaps a cleanup thread could be created for a specific application.

               

              On the other hand, here is a sample code for how to clean up after terminated thread(s):

               

              typedef struct

              {

                   wiced_thread_t tid;

                   wiced_mutex_t lock;

              } threadInfo;

               

              static threadInfo tInfo;

               

              static void test_thread_main(uint32_t arg)

              {

                  threadInfo* tinfoP = (threadInfo*)arg;

                  wiced_thread_t tid = tinfoP->tid;

                  WPRINT_APP_INFO(( "Thread begin %ld\n", (uint32_t)(tid)));

                  wiced_rtos_delay_milliseconds(100);

                  WPRINT_APP_INFO(( "Thread end %ld\n", (uint32_t)(tid)));

                  wiced_rtos_unlock_mutex(&(tinfoP->lock));

                  WICED_END_OF_CURRENT_THREAD();

              }

               

              void application_start(void)

              {

                  wiced_init();

                  wiced_rtos_init_mutex(&tInfo.lock);

               

                  int i;

                  for (i = 0; i < 200; i++)

                  {

                      WPRINT_APP_INFO(( "\r\nThread no:%d\n", i+1));

                      wiced_rtos_lock_mutex(&tInfo.lock);

                      wiced_result_t status = wiced_rtos_create_thread(&(tInfo.tid), WICED_DEFAULT_WORKER_PRIORITY, "test thread",

                                                                       test_thread_main, 5000, (void*)&tInfo);

               

                      if (status != WICED_SUCCESS)

                      {

                          wiced_rtos_unlock_mutex(&tInfo.lock);

                          WPRINT_APP_INFO(( "create thread fail, status=%u\n", status ));

                      }

                      else

                      {

                          wiced_rtos_lock_mutex(&tInfo.lock);

                          wiced_rtos_unlock_mutex(&tInfo.lock);

                          WPRINT_APP_INFO(("CleanUp.\n"));

                          host_rtos_delete_terminated_thread(&(tInfo.tid));

                      }

               

                      wiced_rtos_delay_milliseconds(100);

                  }

              }

               

              Hope it helps,

              Seyhan