6 years, 8 months ago.

mbed-os 5 maximum number of threads

This is the 3rd time I am asking the same question. My earlier two attempts in the same context, didn't receive any response. The questions never appeared under the "Question" tab. While posting the questions, I saw the status "Waiting for moderation". Hence I am not even aware if anybody is attending the question. This time I am hoping that somebody would attend or at least acknowledge. So following is the problem description.

I am testing how many maximum threads may be spawned in Cortex-M4 targets with mbed-os 5.x by default (w/o me changing any configuration). The target is Nucleo-F303RE. But I am sure the problem is not target specific.

1. Online build - This didn't allow me go beyond 6 thread (1 main + 5 threads). The thread start returned non zero when 6th thread creation was attempted. I further investigated and understood that there is a define OS_TASKCNT that decides how many maximum threads may be spawned. The mbed-os source tree revealed that the said (OS_TASKCNT) define doesn't appear any where except under Realtek target file mbed_rtx.h.

2. offline build - This did allow me to go beyond 12 (1 main + 11 threads) threads. While spawning 13th thread got run time error message "Operator new[] out of memory". The mbed-os repository version was the one that was clone under running mbed-cli. i.e "mbed new mbed-os-program". In the repository, I found that OS_TASKCNT was defined only in STM and Realtek targets.

So my questions are: 1. Is OS_TASKCNT still in use and decides maximum number of threads? 2.1. If answer to question #1 is yes, where is OS_TASKCNT defined? 2.2. Why is it defined only for certain target(s)? 2.3. What is the default value for any target? 2.4. What are other defines that are related to OS_TASKCNT, that are required to be configured for reasonable values so as to not get any run time error? I would prefer compile time error, if possible. 3.1 If answer to question #1 is no, how is (max number of threads) defined?

4. Studied the RTX5 code and there are two files that define all OS_ configuration options. But the values there (OS_THREAD_NUM in particular) is doesn't appear to be in sync with the run time results I am getting (mentioned earlier). I also studied mbed_app.json. But if the file is not present what are default values for all OS_* configuration options?

Thanks, Hemant

1 Answer

6 years, 8 months ago.

The only limitation is the amount of memory that you have available on the target, and how big your stack size per thread is.

With default settings a thread takes up about 4.4K of heap space. Thus, on a K64F I can create 40 threads (heap size of 183K). If I set the stack size to 2K per thread I can create 77. All built with GCC_ARM against mbed OS 5.5.

Here's a small program to test it (set the MBED_HEAP_STATS_ENABLED=1 macro beforehand):

#include "mbed.h"
#include "mbed_stats.h"

void wait_forever() {
    wait(osWaitForever);
}

int main() {
    Serial pc(USBTX, USBRX);
    pc.baud(115200);

    mbed_stats_heap_t heap_stats;

    size_t thread_count = 0;

    while (1) {
        mbed_stats_heap_get(&heap_stats);
        printf("Current heap: %lu / %lu\r\n", heap_stats.current_size, heap_stats.reserved_size);

        Thread* t = new Thread(osPriorityNormal, 2*1024);
        t->start(&wait_forever);

        printf("Thread count is now %d\n", ++thread_count);

        wait_ms(1000);
    }
}

Regarding OS_TASKCNT, I'm not sure. In mbed OS 5.4 and mbed OS 5.5 it's not used anymore, so I guess the limit was removed and now the only limit is available memory.

Accepted Answer

Thanks Jan for clarification. On Nucleo-F303RE your program (with 2048KB heap space per thread) could create 24 threads. This is exactly half of what I was getting with default heap per thread.

posted by Hemant Joshi 02 Aug 2017