pico-sdk
pico-sdk copied to clipboard
C++ support for std::thread
Hi, I have some code built for Linux and ESP32 which use the C++ classes which wrap pthread and others. I want to use this code on a RP2040 in FreeRTOS SMP.
I added Lab-Project-FreeRTOS-POSIX include and source files and can confirm the pthreads functions work.
void* threadFunction(void* arg) {
while ( 1 ) {
std::cout << "pthread is working!" << std::endl;
printf("pthread is working!\n");
vTaskDelay(1000);
}
return nullptr;
}
...
pthread_t thread;
// Create a new thread that runs 'threadFunction'
if (pthread_create(&thread, nullptr, threadFunction, nullptr) != 0) {
std::cerr << "Error: pthread_create failed!" << std::endl;
return 1;
}
...
std::thread things however do not compile. (confirming #include <thread> is there)
The error when I try to declare one is:
system.cpp:408:10: error: 'thread' in namespace 'std' does not name a type
408 | std::thread thread_;
| ^~~~~~
When I try to straight up use one, eg.
std::thread t([]{ std::cout << "Threading is supported!\n"; });
t.join();
the error is
system.cpp:430:14: error: 'thread' is not a member of 'std'
430 | std::thread t([]{ std::cout << "Threading is supported!\n"; });
So it really looks like it doesn't want to see it!
In Linux environments, the magic -pthread option turns the multithreading lights on, but I guess that's not the right approach here.
I have half a day into this now, and have been having the "almost there" feeling for the whole time. I started delving into the source code for std::thread and quickly realized I was in over my head. So I think it's time to ask for help.
Is there something simple I can add / define in FreeRTOSConfig.h, CMakeLists.txt or anywhere else to magically turn the lights on?
I'm happy to make a minimal example and dump code in here, but I'm kinda hoping there's a "Oh! You have to define XXXX to make it work" answer.
If it is easy / obvious, it seems to me that allowing this as a pico option somewhere would be really great and would unlock a lot of C++ compatibility.