Running multiple thread instances of the same function. (See MultiThread Program for an example on how to use it)

Dependents:   MutliThread Server_Multi_Client HelloWorld C027_SupportTest ... more

Revision:
3:d370bed31f45
Parent:
2:46b56c9086f2
--- a/Threads.cpp	Sat Jun 29 22:05:28 2013 +0000
+++ b/Threads.cpp	Sat Jun 29 22:59:46 2013 +0000
@@ -17,10 +17,13 @@
 #include "Threads.h"
 
 /*Initialize memory and set the pointer to the thread*/
-int initThread(ThreadList **addresOfTL,os_pthread pthread,ThreadList** addresOfThread)
+int initThread(ThreadList **addresOfTL,os_pthread pthread,ThreadList** addresOfThread,int max)
 {
+    
     if(*addresOfTL==NULL)
     {
+        if(max<=0)
+            return 0;
         *addresOfTL=(ThreadList*)malloc(sizeof(ThreadList));
         if(*addresOfTL==NULL)
             return 0;
@@ -45,6 +48,32 @@
         return 1;
     }
     else
-        return initThread(&((*addresOfTL)->nextThread),pthread,addresOfThread);
+    {
+        if((*addresOfTL)->thread->tcb.state == 0)
+        {
+            free((*addresOfTL)->thread->stack_pointer);
+            (*addresOfTL)->thread->stack_pointer=NULL;
+            free((*addresOfTL)->thread);
+            (*addresOfTL)->thread=NULL;
+            (*addresOfTL)->thread=(osThreadDef_t *)malloc(sizeof(osThreadDef_t ));
+    
+            if((*addresOfTL)->thread==NULL)
+                return 0;
+        
+            (*addresOfTL)->thread->pthread=pthread;
+            (*addresOfTL)->thread->tpriority=osPriorityNormal;
+            (*addresOfTL)->thread->stacksize=DEFAULT_STACK_SIZE;
+    
+            (*addresOfTL)->thread->stack_pointer=NULL;
+            (*addresOfTL)->thread->stack_pointer=(unsigned char *)malloc(DEFAULT_STACK_SIZE);
+    
+            if((*addresOfTL)->thread->stack_pointer==NULL)
+                return 0;
+            (*addresOfThread)=(*addresOfTL);
+            return 1;
+        }
+        else
+            return initThread(&((*addresOfTL)->nextThread),pthread,addresOfThread,--max);
+    }
 }