An example of running multiple thread instances of the same function. Memory is being allocated dynamically.

Dependencies:   Threads mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
lemniskata
Date:
Sat Jun 29 21:14:20 2013 +0000
Child:
1:458d82e37147
Commit message:
Creating and running multiple thread instances of the same function

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
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Threads.lib	Sat Jun 29 21:14:20 2013 +0000
@@ -0,0 +1,1 @@
+Threads#266b0dc4f8d0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Jun 29 21:14:20 2013 +0000
@@ -0,0 +1,45 @@
+#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() {
+
+     osThreadDef_t **my_threads=NULL;
+     if(initThread(&my_threads,Do)==0)
+     {
+        pc.printf("Thread creation faile\n");
+        return -1;
+     }
+     int i=1;
+     osThreadCreate(my_threads[i-1],(void *) i);
+    while(1) {
+         i++; 
+        if(i<6)
+        {
+             if(reAlloc(&my_threads,Do,i)==0)
+            {
+                pc.printf("Thread creation faile\n");
+                return -1;
+            }
+            osThreadCreate(my_threads[i-1],(void *) i);
+            i++;
+        }
+        wait(0.2);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Sat Jun 29 21:14:20 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#58b30ac3f00e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Jun 29 21:14:20 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/b3110cd2dd17
\ No newline at end of file