6 years, 7 months ago.

Using Multiple Thread?

Im using a module NXP 1781 and using SPI to access up to 8 x PCA9555 to have up to 80 x digital Outputs and Inputs. My solution uses Ethernet port to access a IHM as cliente TCP/IP. It hardware was used in other projects w/o problem, but, the developer used a desktop IDE and old mbed-rtos.

I created new Project and I need to create multiple Threads running in parallell. To minimize the I2C traphic I want to create a I2C comm thread to share communication with PCA9555 w/o need use sepalhore.

Other thread will be used to control communication between module and IHM over TCP/IP as Client.

The problem is that all Thread examples I used in my Project fails to work. It only Works individually into OnLine builder.

Do you have any suggestion?

Can I have 10 threads running in parallel? What I have to do to prevents overload in proccessing?

Regards.

Question relating to:

Examples for mbed OS

1 Answer

6 years, 7 months ago.

Hi Jose,

Here's an example of 2 threads in Mbed OS, one in main() and the other in a separate function led2_thread(): https://os.mbed.com/docs/v5.8/reference/thread.html#thread-example

You can also explicitly create multiple Thread variables:

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
Thread thread1;
Thread thread2;

void led1_thread() {
    while (true) {
        led1 = !led1;
        wait(0.5);
    }
}
 
void led2_thread() {
    while (true) {
        led2 = !led2;
        wait(1);
    }
}
 
int main() {
    thread1.start(led1_thread);
    thread2.start(led2_thread);
}

Please let me know if you have any questions!

- Jenny, team Mbed

Thanks for your answer. I Firstly tryed it exactly example and in my case didnt Works. After working hard I Found the solution bellow at: https://www.embarcados.com.br/threads-no-mbed-os/

STEP 01: At Header File Defined stack size Local Buffers

unsigned char tcp_stk[2048]; unsigned char i2c_stk[1024];

STEP 02: At Header File Defined Threads using Priorityes(Normal) is save and Stack Size defined above

Thread thTCP_Comm(osPriorityNormal, 2048, &tcp_stk[0]);

Thread thI2C_Comm(osPriorityNormal, 1024, &i2c_stk[0]);

STEP 03:At Code Created threads pointing to Threads Entry

thTCP_Comm.start(TCP_CommThread);

thI2C_Comm.start(I2C_CommThread);

STEP 04:At Code Created threads

void TCP_CommThread(void *pvArgs)

{

Thread start code

while(1)

{

Thread loop code

}

}

void I2C_CommThread(void *pvArgs) {

Thread start code

while(1) {

Thread loop code

}

}

It is working w/o problem now!!!!!

By the way, thanks for your help!!!!!

posted by Jose Carlos de Oliveira 30 Mar 2018