Creating threads in an C++ library for ESP32

Hi,

In an university project I need to implement a package resending for the CoAP-simple library for arduino. Theoretically, I would consider this an easy task, since it only envolves a few lines of code. As I am more of a Java programmer and not very well versed in C++ and arduino stuff I am a bit desperate with the error I have got since I cannot find a solution to it.

In my code, if a confirmable packet is sent, I want to create a thread which sleeps for a timeout, then checks if the packet needs to be resent and then possibly retransmits it. I have already tested creating and calling a thread like that in a separate C++ program and it worked flawlessly.

The problems began when trying to include it into the actual library. While compiling and everything works, as soon as my library tries to create threads I get the following error in the serial monitor:

14:35:19.810 -> Send Request
14:35:19.843 -> abort() was called at PC 0x40136f9f on core 1
14:35:19.876 -> 
14:35:19.876 -> Backtrace: 0x4008ec3c:0x3ffb1d10 0x4008ee6d:0x3ffb1d30 0x40136f9f:0x3ffb1d50 0x40136fe6:0x3ffb1d70 0x400d2a37:0x3ffb1d90 0x400d2d69:0x3ffb1dd0 0x400d2e42:0x3ffb1ef0 0x400d2e71:0x3ffb1f40 0x400d11a7:0x3ffb1f80 0x400d3a65:0x3ffb1fb0 0x4008b009:0x3ffb1fd0
14:35:20.141 -> 
14:35:20.141 -> Rebooting...

What is happening in my code is that, when sending a message, the function resend is called that is supposed to create a new thread for the resending. In a try to eliminate variables while trying to find the error cause it was reduced to the code below:

library cpp file (includes header file):

int Coap::resend(int delay) {

    test_thread.reset(new std::thread(test));

    return 0;
}

library header file:

class Coap {
private:
    std::unique_ptr<std::thread> test_thread;

[...]
}

static void test() {
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    std::cout << "Thread created!";
}

I have tried searching for the error but most cases do barely apply to my use case and one that i found has been applied, but did not help me: https://github.com/espressif/arduino-esp32/issues/2814
The library I'm working with is this one: GitHub - hirotakaster/CoAP-simple-library
I'm using two ESP32 WROVER modules with one arduino client and one arduino server implementation which are connected to each other over my smartphones hotspot network. Communication has worked flawlessly without the thread creation.

My gut feeling is telling me that it is an issue that I cannot solve without alot of indepth knowledge, which is why I'm asking you to help me.
Thank you for your time!

Well, as i guessed nobody would answer since this is a rather complicated niche case.

After fiddling around and trying a lot of stuff I came back to the github issue where a patient developer tries to cope with a rather ignorant individuum (thank you, ignorant individuum and patient developer, without you I might never have solved this!). This were the steps I went throught:

  • Create an Arduino test program which just prints to serial from main loop and a std::thread --> worked flawlessly
  • Move thread creation into a library that just provides a thread-starting function --> crashes
  • Check the github issue again as it is my only lead
  • Adjust the fix provided there to my specific problem, by creating a global std::thread pointer in the arduino program which then is passed to the class provided by the library which uses that pointer to create a thread --> works!

Therefore, the solution was right in front of me in the github-issue conversation, just needed to be adjusted to work with the library:

I suspect the reason those std::thread objects needed to be global is so they don't go out of scope when setup() completes.

I would be glad if anyone out there could provide me with a more detailed explaination of what is happening there. If anybody finds this in a similar desperate state as I was in (deadline approaching x.x), I hope I was able to help you.

Have a nice day (: