5 years ago.  This question has been closed. Reason: Off Topic

How can i use Thread correctly ?

Hello,

I'm using 2 Threads in my program, one to write something on a Serial Output and a second to measure something. But i think there is a problem with Thread. Normally when a Thread is running it is in running state and the other are in waiting or Ready state.

/media/uploads/AlexandreBurkert/threadstatus.png

In my program the first Thread which write something on Serial Output has a "wait(5)". This means it will wait 5s and normally this Thread pass into waiting state. During this time normally the second Thread must run and read the different sensor inputs.

include the mbed library with this snippet

//*****threadAffichage()*****
void threadAffichage(){
    while(1){
        //*****     
        pc1.printf("===================\r\n");
        pc1.printf("hall1Moy : %f\r\n",hall1Moy);
        pc1.printf("hall2Moy : %f\r\n",hall2Moy);
        pc1.printf("hall3Moy : %f\r\n",hall3Moy);
        pc1.printf("hall4Moy : %f\r\n",hall4Moy);
        pc1.printf("hall5Moy : %f\r\n",hall5Moy);
        pc1.printf("dbg : %d\r\n",dbg);
        pc1.printf("===================\r\n");
        wait(5);
        //*****
    }
}
//*************************

//*****threadHallMoyenne()*****
void threadHallMoyenne(){
    while(1){
        //***** 
        hall1Moy=hallSensorMesureMoyenne(hall_1,hall1Moyenne,AVERAGE_MESURE_NUMBERS);
        hall2Moy=hallSensorMesureMoyenne(hall_2,hall2Moyenne,AVERAGE_MESURE_NUMBERS);
        hall3Moy=hallSensorMesureMoyenne(hall_3,hall3Moyenne,AVERAGE_MESURE_NUMBERS);
        hall4Moy=hallSensorMesureMoyenne(hall_4,hall4Moyenne,AVERAGE_MESURE_NUMBERS);
        hall5Moy=hallSensorMesureMoyenne(hall_5,hall5Moyenne,AVERAGE_MESURE_NUMBERS);
        dbg++;
        wait(0.1);
        //***** 
    }
}


//=====Main()=====
int main(){
    Affichage.start(threadAffichage);
    HallMoyenne.start(threadHallMoyenne);
}
//=========================

My questions are : Why during the "wait(5)" from the first Thread, the second Thread doesn't run more than one loop ? Why the Thread state don't update ?

I read that : /media/uploads/AlexandreBurkert/capture.png

P.S : What I want to do is to run two task in parallel.

1 Answer

5 years ago.

Are you sure you're calling the right wait? Just reading what you posted, it seems like mbed is warning you that the wait() function will put the calling thread in a busy loop but will not let the thread be pre-empted, and that Thread::wait() will allow your calling thread to be pre-empted.

I'd really like to see this code fail with Thread::wait(), and then we can dig deeper.

Accepted Answer