Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ support for std::thread #1901

Open
davidthings opened this issue Sep 4, 2024 · 0 comments
Open

C++ support for std::thread #1901

davidthings opened this issue Sep 4, 2024 · 0 comments

Comments

@davidthings
Copy link

davidthings commented Sep 4, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant