C++ support for std::thread #1901

Open
opened 2024-09-03 22:51:46 -05:00 by davidthings · 1 comment
davidthings commented 2024-09-03 22:51:46 -05:00 (Migrated from github.com)

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.

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. ``` C++ 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. ``` C++ 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.
teknoman117 commented 2025-09-09 01:51:01 -05:00 (Migrated from github.com)

Unfortunately there's no easy answer for this one. Most of the C++ standard library isn't available for embedded targets. See the difference between libstdc++ "freestanding" versus "hosted" mode.

Embedded toolchains come with freestanding because it's the subset of the standard library which can made available without an operating system. The rest of the standard library requires dynamic allocation, exceptions, threading support, etc. Someone would have to implement the C++ standard library for FreeRTOS.

This is so much larger than a pico-sdk issue.

The same goes for #2557

Unfortunately there's no easy answer for this one. Most of the C++ standard library isn't available for embedded targets. See the difference between libstdc++ "freestanding" versus "hosted" mode. Embedded toolchains come with freestanding because it's the subset of the standard library which can made available without an operating system. The rest of the standard library requires dynamic allocation, exceptions, threading support, etc. Someone would have to implement the C++ standard library for FreeRTOS. This is so much larger than a pico-sdk issue. The same goes for #2557
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-raspberrypi/pico-sdk#1901
No description provided.