5 years, 9 months ago.

thread doesn't work in fun() except main

there are two code setions:

1

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
 
void led2_thread(void const *args) {
    while (true) {
        led2 = !led2;
        Thread::wait(1000);
    }
}
 
void StartTheThread(void)
{
    thread1.start(led2_thread); 
} 
int main() 
{
  StartTheThread(); 
   
    while (true) 
    {
        led1 = !led1;
        Thread::wait(500);
    }
}

2

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
 
void led2_thread(void const *args) {
    while (true) {
        led2 = !led2;
        Thread::wait(1000);
    }
}
 
int main() 
{
  thread1.start(led2_thread); 
    while (true) 
    {
        led1 = !led1;
        Thread::wait(500);
    }
}

then , code 1 do not action correctly ,but code 2 work .why?

1 Answer

5 years, 9 months ago.

Hello Shang,

Can you verify that code again please? From just looking at it, it seem you never initialize a Thread object called thread1 so both of the codes should not even compile in the first place. If you were to initialize thread1 object, then the code should work.

1 fixed

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
Thread thread1;
 
void led2_thread() {
    while (true) {
        led2 = !led2;
        wait(1);
    }
}
 
 void StartTheThread(void)
{
     thread1.start(led2_thread);
} 
 
int main() {
    StartTheThread();
    while (true) {
        led1 = !led1;
        wait(0.5);
    }
}

2 fixed

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
Thread thread1;
 
void led2_thread() {
    while (true) {
        led2 = !led2;
        wait(1);
    }
}
 
int main() {
    thread1.start(led2_thread);
    while (true) {
        led1 = !led1;
        wait(0.5);
    }
}

Please let me know if you have any questions!

- Peter, team Mbed

If this solved your question, please make sure to click the "Thanks" link below!

Accepted Answer

Oh, yes, I lost it.Thank you for your answer.

However, I wouldn't initialize a Thread at begin of the code, just like it:

  1. include "mbed.h"

DigitalOut led1(LED1); DigitalOut led2(LED2);

void led2_thread() { while (true) { led2 = !led2; wait(1); } }

void StartTheThread(void) { Thread ledthread(osPriorityNormal, 512, NULL, "led_thread"); ledthread.start(led2_thread); }

int main() { StartTheThread(); while (true) { led1 = !led1; wait(0.5); } }

fun "led2_thread" does not action any more.

posted by shang dong 22 Jun 2018

Hi Shang,

The reason your code does not work is because you initialize the thread inside your function StartTheThread(). This will create a local thread variable which will get destroy once you exit out of the function. Therefore, the ledthread no longer exist, which is why led2_thread does not run at all. You need to either initialize your thread in main and passed its pointer into your function or initialize it as a global variable like I did in the code above.

Please let me know if you have any questions!

- Peter, team Mbed

If this solved your question, please make sure to click the "Thanks" link below!

posted by Peter Nguyen 22 Jun 2018

Hi Peter, thanks for your answer. From this ,the thread "main" is a special thread which is running forever. I think it is different from other RTOS who can create thread any time. I should learn more about Mbed os.

posted by shang dong 23 Jun 2018

Hi Peter, there is another question. I want to start a thread in other .cpp so that I can manage my project .But it seems doesn't work. Is it right?

posted by shang dong 03 Jul 2018

So when dealing with thread, you need some kind of data structure to manage all your threads. I would recommend you use a global array or hash table for your every thread you created. That way, when you create a thread using another .cpp file, the thread still share across all .cpp file

posted by Peter Nguyen 03 Jul 2018