Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 10 months ago.
Thread interleaving question
Hi, I'm working on some threaded code. One thread processes input/output, while the main thread executes long_add, a dummy function. Code here:
main.cpp
#include "mbed.h" #include "MODSERIAL.h" #include "rtos.h" DigitalOut led1(LED1); DigitalOut led2(LED2); MODSERIAL In(p13, p14); // serial object to read data coming in MODSERIAL Out(p9, p10); // serial object to send data MODSERIAL pc(USBTX, USBRX); Timer in_timer; Timer out_timer; Timer print_timer; int dataIn[32]; int dataOut[32]; osThreadId mainThreadID; void io_thread(void const *args) { for(int ii=0; ii<32; ii++) { // pc.printf("ii is %d\r\n", ii); Out.putc(ii); } int jj = 0; while (In.readable() && jj < 32) { //pc.printf("jj is %d\r\n", jj); dataIn[jj]=In.getc(); jj++; } pc.printf("Time in io_thread: %f \r\n", (float)out_timer.read_us()); osSignalSet(mainThreadID, 0x1); } void long_add() { int i = 0; while ( i < 10000) { int j = 0; while (j < 100) { j += 1; } i += 1; } } int main() { mainThreadID = osThreadGetId(); // pc.baud(9600); In.baud(921600); Out.baud(921600); pc.baud(921600); in_timer.start(); long_add(); pc.printf("In time: %f \r\n", (float)in_timer.read_us()); out_timer.start(); Thread thread(out_thread); long_add(); osSignalWait(0x1, osWaitForever); // float temp2=out_timer.read_us(); //pc.printf("In time: %f \r\n", (float)out_timer.read_us()); }
By itself, the I/O thread function takes 450 us and long_add takes 417 us. However, with threading long_add takes only 420 us, while the I/O function takes an additional 600 us to run; I expected long_add would finish around 900 due to the interleaving, but it seems as if there's barely any interleaving happening. Is this a thread scheduling quirk, or is there something wrong with our implementation?