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

Files at this revision

API Documentation at this revision

Comitter:
lemniskata
Date:
Sat Jun 29 22:59:46 2013 +0000
Parent:
2:46b56c9086f2
Commit message:
Running multiple thread instances of the same function. New thread is started only if there are less than MAX non-inactive threads

Changed in this revision

Threads.cpp Show annotated file Show diff for this revision Revisions of this file
Threads.h Show annotated file Show diff for this revision Revisions of this file
--- 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);
+    }
 }
 
--- a/Threads.h	Sat Jun 29 22:05:28 2013 +0000
+++ b/Threads.h	Sat Jun 29 22:59:46 2013 +0000
@@ -27,6 +27,6 @@
     struct ThreadList* nextThread;
 }ThreadList;
 
-int initThread(ThreadList **addresOfTL,os_pthread pthread,ThreadList** addresOfThread);
+int initThread(ThreadList **addresOfTL,os_pthread pthread,ThreadList** addresOfThread,int max);
 
 #endif