An example of running multiple thread instances of the same function. Memory is being allocated dynamically.
Dependencies: Threads mbed-rtos mbed
main.cpp
- Committer:
- lemniskata
- Date:
- 2013-06-29
- Revision:
- 3:681b2f7e1b41
- Parent:
- 1:458d82e37147
- Child:
- 4:a25f2646a1bc
File content as of revision 3:681b2f7e1b41:
#include "mbed.h" #include "Threads.h" Serial pc(USBTX, USBRX); osMutexId stdio_mutex; osMutexDef(stdio_mutex); void Do(void const *data) { int i=(int)data; while(1) { osMutexWait(stdio_mutex, osWaitForever); pc.printf("This is Thread #%d\n",i); osMutexRelease(stdio_mutex); wait(1); } } int main() { ThreadList* my_threads=NULL; //List of all Initialized threads ThreadList* thread; //pointer to the last created ThreadList element int i=0; while(1) { //We want 6 instances of the Do() function to run in separate threads if(i<6) { //Initialize a thread if(initThread(&my_threads,Do,&thread)==0) { pc.printf("Thread creation faile\n"); return -1; } //Start the thread and store the id thread->id=osThreadCreate(thread->thread,(void *) i); i++; } wait(0.2); } }