An example of running multiple thread instances of the same function. Memory is being allocated dynamically.
Dependencies: Threads mbed-rtos mbed
Revision 3:681b2f7e1b41, committed 2013-06-29
- Comitter:
- lemniskata
- Date:
- Sat Jun 29 22:06:29 2013 +0000
- Parent:
- 2:78d8f8cac107
- Child:
- 4:a25f2646a1bc
- Commit message:
- Initializing and running multiple thread instances from the same function. All threads are stored in a list with their osThreadId's
Changed in this revision
Threads.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/Threads.lib Sat Jun 29 21:19:53 2013 +0000 +++ b/Threads.lib Sat Jun 29 22:06:29 2013 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/lemniskata/code/Threads/#ecdd97ea3d3b +http://mbed.org/users/lemniskata/code/Threads/#46b56c9086f2
--- a/main.cpp Sat Jun 29 21:19:53 2013 +0000 +++ b/main.cpp Sat Jun 29 22:06:29 2013 +0000 @@ -19,32 +19,23 @@ } int main() { - - //Create an array of pointers to threads - osThreadDef_t **my_threads=NULL; + ThreadList* my_threads=NULL; //List of all Initialized threads + ThreadList* thread; //pointer to the last created ThreadList element - //Initialize the first thread of the function Do() - if(initThread(&my_threads,Do)==0) - { - pc.printf("Thread creation faile\n"); - return -1; - } - int i=1; - - //Start the first thread - osThreadCreate(my_threads[i-1],(void *) i); - while(1) { - i++; + int i=0; + while(1) + { + //We want 6 instances of the Do() function to run in separate threads if(i<6) { - //Reallocate memory for the next thread of the function Do() - if(reAlloc(&my_threads,Do,i)==0) + //Initialize a thread + if(initThread(&my_threads,Do,&thread)==0) { pc.printf("Thread creation faile\n"); return -1; } - //Start the next thread - osThreadCreate(my_threads[i-1],(void *) i); + //Start the thread and store the id + thread->id=osThreadCreate(thread->thread,(void *) i); i++; } wait(0.2);