Thread start hangs

13 Jan 2018

I can successfully start a single thread (plus the main thread). I am now attempting to start another thread (i.e. 2 Threads + the main thread) but the second call to start just hangs forever.

#include <mbed.h>
#include <rtos.h>

Thread a;
Thread b;

void thread_a() {
  while (true) {
    Thread::wait(1000);
  }
}

void thread_b() {
  while (true) {
    Thread::wait(1000);
  }
}

int main() {
  a.start(callback(thread_a));
  b.start(callback(thread_b));

  while (true) {
    Thread::wait(1000);
  }

  return 0;
}

The call to a.start returns 0 but the call to b.start never returns. I'm using an STM32F103C8 "blue pill" board.

-Scott

22 Jan 2018

Can you check the terminal, does it print anything, any error ? Or any output there? I would expect if this hangs, it either fails to create a second thread, might lead to an runtime error.

Let us know

22 Jan 2018

Note that the ST32F103C8 has 64KB flash and 20KB RAM. You have probably selected the F103 nucleo as the target in the online compiler. The nucleo target has more memory. Check that the build code doesnt exceed the available flash/RAM.

22 Jan 2018

I've tried the example threading code and it works. The mbed version was the actual mbed-dev (MBED_LIBRARY_VERSION 158), target mcu is BLUEPILL_F103C8. You will get an error because of an undefined target in ./mbed-os/targets/TARGET_STM/mbed_rtx4.h. The F103C8 and F103RB differ only in Flashsize, so setting it to both target is ok, change:

#elif defined(TARGET_STM32F103RB)

to:

#elif defined(TARGET_STM32F103RB) || defined(TARGET_STM32F103C8)

For testing I have exported the sample to gnuarmeclipse, the threads are running.

@Wim: the nucleo with F103RB has only more flash, ram size 20 kB is the same. In fact, the C8 are also RB and have 128 kB flash, openOCD or stm32flash can write the whole flash.

09 Aug 2019

Hey i just was facing the same issue.

Device: Blue pill stm32 f013C8T6

Platformio mbed

I think the reason is - the stack size

You have 3 thread here main thread 4k stack a thread 4k stack b thread 4k stack total 12 K stack so when thread b tries allocate stack - it fails.

try this

set stack size

Thread a(osPriorityNormal, 1024);
Thread b(osPriorityNormal, 1024);