An example of running multiple thread instances of the same function. Memory is being allocated dynamically.
Dependencies: Threads mbed-rtos mbed
main.cpp@4:a25f2646a1bc, 2013-06-29 (annotated)
- Committer:
- lemniskata
- Date:
- Sat Jun 29 23:00:27 2013 +0000
- Revision:
- 4:a25f2646a1bc
- Parent:
- 3:681b2f7e1b41
Creating multiple thread instances of the same function. New thread is created only if there are less than MAX non-inactive threads.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lemniskata | 0:06fdc09661c2 | 1 | #include "mbed.h" |
lemniskata | 0:06fdc09661c2 | 2 | #include "Threads.h" |
lemniskata | 4:a25f2646a1bc | 3 | /* |
lemniskata | 4:a25f2646a1bc | 4 | Author: Ivan Shindev |
lemniskata | 4:a25f2646a1bc | 5 | Date: 06/29/2013 |
lemniskata | 0:06fdc09661c2 | 6 | |
lemniskata | 4:a25f2646a1bc | 7 | This program creates multiple thread instances of the same function |
lemniskata | 4:a25f2646a1bc | 8 | Each thread prints its number and terminates after 5 prints |
lemniskata | 4:a25f2646a1bc | 9 | A new thread of the same function is created only if less |
lemniskata | 4:a25f2646a1bc | 10 | than 5 threats are currently non-inactive |
lemniskata | 4:a25f2646a1bc | 11 | |
lemniskata | 4:a25f2646a1bc | 12 | Mbed RTOS does not support multiple thread instances of the same function |
lemniskata | 4:a25f2646a1bc | 13 | with dynamic memory |
lemniskata | 4:a25f2646a1bc | 14 | |
lemniskata | 4:a25f2646a1bc | 15 | Start the program and watch the miracle :) |
lemniskata | 4:a25f2646a1bc | 16 | */ |
lemniskata | 0:06fdc09661c2 | 17 | |
lemniskata | 0:06fdc09661c2 | 18 | Serial pc(USBTX, USBRX); |
lemniskata | 0:06fdc09661c2 | 19 | osMutexId stdio_mutex; |
lemniskata | 0:06fdc09661c2 | 20 | osMutexDef(stdio_mutex); |
lemniskata | 0:06fdc09661c2 | 21 | |
lemniskata | 4:a25f2646a1bc | 22 | |
lemniskata | 0:06fdc09661c2 | 23 | void Do(void const *data) { |
lemniskata | 0:06fdc09661c2 | 24 | |
lemniskata | 0:06fdc09661c2 | 25 | int i=(int)data; |
lemniskata | 4:a25f2646a1bc | 26 | int exec=0; |
lemniskata | 0:06fdc09661c2 | 27 | while(1) |
lemniskata | 0:06fdc09661c2 | 28 | { |
lemniskata | 0:06fdc09661c2 | 29 | osMutexWait(stdio_mutex, osWaitForever); |
lemniskata | 0:06fdc09661c2 | 30 | pc.printf("This is Thread #%d\n",i); |
lemniskata | 0:06fdc09661c2 | 31 | osMutexRelease(stdio_mutex); |
lemniskata | 4:a25f2646a1bc | 32 | exec++; |
lemniskata | 4:a25f2646a1bc | 33 | if(exec>=5) //end the thread after 10 executions |
lemniskata | 4:a25f2646a1bc | 34 | break; |
lemniskata | 0:06fdc09661c2 | 35 | wait(1); |
lemniskata | 0:06fdc09661c2 | 36 | } |
lemniskata | 0:06fdc09661c2 | 37 | } |
lemniskata | 0:06fdc09661c2 | 38 | |
lemniskata | 0:06fdc09661c2 | 39 | int main() { |
lemniskata | 3:681b2f7e1b41 | 40 | ThreadList* my_threads=NULL; //List of all Initialized threads |
lemniskata | 3:681b2f7e1b41 | 41 | ThreadList* thread; //pointer to the last created ThreadList element |
lemniskata | 1:458d82e37147 | 42 | |
lemniskata | 4:a25f2646a1bc | 43 | int max=5; |
lemniskata | 4:a25f2646a1bc | 44 | int i=1; |
lemniskata | 3:681b2f7e1b41 | 45 | while(1) |
lemniskata | 3:681b2f7e1b41 | 46 | { |
lemniskata | 4:a25f2646a1bc | 47 | |
lemniskata | 4:a25f2646a1bc | 48 | //Initialize a thread. |
lemniskata | 4:a25f2646a1bc | 49 | //A new thread is created only if the list has free space or |
lemniskata | 4:a25f2646a1bc | 50 | // one of started threads has finishid |
lemniskata | 4:a25f2646a1bc | 51 | if(initThread(&my_threads,Do,&thread,max)==0) |
lemniskata | 0:06fdc09661c2 | 52 | { |
lemniskata | 4:a25f2646a1bc | 53 | // pc.printf("Thread creation failed. \n"); |
lemniskata | 4:a25f2646a1bc | 54 | |
lemniskata | 0:06fdc09661c2 | 55 | } |
lemniskata | 4:a25f2646a1bc | 56 | else |
lemniskata | 4:a25f2646a1bc | 57 | { |
lemniskata | 4:a25f2646a1bc | 58 | //Start the thread and store the id |
lemniskata | 4:a25f2646a1bc | 59 | thread->id=osThreadCreate(thread->thread,(void *) i); |
lemniskata | 4:a25f2646a1bc | 60 | i++; |
lemniskata | 4:a25f2646a1bc | 61 | } |
lemniskata | 4:a25f2646a1bc | 62 | |
lemniskata | 0:06fdc09661c2 | 63 | wait(0.2); |
lemniskata | 0:06fdc09661c2 | 64 | } |
lemniskata | 0:06fdc09661c2 | 65 | } |