10 years, 1 month ago.

RTOS - passing struct as argument to thread

I want to pass more than one argument to a Thread using RTOS.

To do this I start the thread and pass as argument a struct (which consists of more than one actual argument).

my code compiles but it does not what I want(no serial print, no led blinking). any ideas what i do wrong? thank you

Import programexperiment_with_rtos

experiment_with_rtos

code of the above link program:

#include "mbed.h"
#include "rtos.h"
//#include <string>
 
DigitalOut led2(LED2);
Serial pc(USBTX, USBRX);
 
//string mes;
 
struct argStruct{
    int delay;
    int message;
}initArgs;
 
 
 //--------------------------------
void led2_thread(void const *args) {
    
    argStruct* rcvArgs=(argStruct*)args;
    pc.printf("delay=%d message=%d\n",rcvArgs->delay,rcvArgs->message);
    while (true) {
        led2 = !led2;
        Thread::wait(rcvArgs->delay);
    }
}
//---------------------------------- 
int main() {
    pc.printf("START!\n");
    initArgs.delay=300;
    initArgs.message=111;
    
    //led2_thread(&initArgs);   //<--this instead of the below line works fine
    Thread thread(led2_thread,&initArgs);  
    pc.printf("END!\n");
 
    }
 

ok never mind i solved it. seems like the main thread should not end before the other threads end. so inserting something like while(1); on line 35 makes the program having the expected behaviour.

posted by foivos Hristou 10 Oct 2014

1 Answer

10 years, 1 month ago.

You can just add Thread::wait( osWaitForever ); to the end of the main function.

Accepted Answer