6 years, 3 months ago.

How can I use Several of Threads at the same time?

I want to use some threads at the same time. so I make code like below. when it works, just Main thread was working, th1 and th2 never worked.(1) when I remove Priority and memories, it means just init Thread "new Thread()", (2), all of threads worked properly.

When I use thread, don't I have to control Threads' Priority?

-code- int main() { 1. Thread *th1 = new Thread((osPriority_t)osPriorityLow7,(uint32_t)0x2000); Thread *th2 = new Thread((osPriority_t)osPriorityBelowNormal,(uint32_t)0x2000); 2. Thread *th1 = new Thread(); Thread *th2 = new Thread();

th1->start(init); th2->start(serialToGw); while(1) { printf("-Main-\r\n"); wait_ms(3000); } }

1 Answer

6 years, 3 months ago.

Hello Jaemyeong,

The code below works fine on LPC1768:

#include "mbed.h"

void init()
{
    while (1) {
        printf("th1\r\n");
        wait_ms(1000);
    }
}

void serialToGw()
{
    while (1) {
        printf("th2\r\n");
        wait_ms(2000);
    }
}

int main()
{
    Thread*     th1 = new Thread((osPriority_t) osPriorityLow7, (uint32_t) 0x2000);
    Thread*     th2 = new Thread((osPriority_t) osPriorityBelowNormal, (uint32_t) 0x2000);

    th1->start(init);
    th2->start(serialToGw);
    while (1) {
        printf("main\r\n");
        wait_ms(3000);
    }
}

You can try to use OS_STACK_SIZE if (uint32_t) 0x2000) doesn't work.

NOTE: To get your code formatted in the question above try to enclose it with code tags as below (each on separate line)

<<code>>
source code 
of your program
<</code>>