Reputation: 43
Are threads(or pthreads) supported on bare-metal arm devices. I am using ARM/GNU C++(arm-none-eabi-g++)compiler and I am getting the error "cannot find -lpthread" upon compilation though I have given the flag -lpthread in linker command.
Upvotes: 3
Views: 4254
Reputation: 1781
If you do not need really pthreads (just you were not aware of other techniques), then coroutines might be a solution for you.
A coroutine is a function that can suspend execution to be resumed later. Coroutines are stackless: they suspend execution by returning to the caller and the data that is required to resume execution is stored separately from the stack. This allows for sequential code that executes asynchronously (e.g. to handle non-blocking I/O without explicit callbacks), and also supports algorithms on lazy-computed infinite sequences and other uses.
https://en.cppreference.com/w/cpp/language/coroutines
In simple terms it's something between functions and threads. You do not have to wait for them to finish executing like regular functions, and do not have to be part of an OS (FreeRTOS is an OS) to have a thread (pthread or other) feature.
This is a new C++20 feature that you could have supported on your embedded toolchain. However, there is a nice blog post on implementing them in a C language.
http://www.vishalchovatiya.com/coroutine-in-c-language/
I'm not including the content from the blog as it's not a small snippet, but I made a redundant snapshot just in case the original website will go down:
https://web.archive.org/web/20210428125420/http://www.vishalchovatiya.com/coroutine-in-c-language/
Upvotes: 2
Reputation: 5161
Without an operating system which supports preemptive multitasking you do not have threads. This is what the term "bare metal" refers to.
Neither g++ nor the linker can give you thread functionality. Only an operating system can provide threads.
You can of course build your own thread handling in your own bare-metal application, but this is a substantial task.
Interrupt routines usually preempt the main program on bare-metal devices and can be thought of as periodically triggered threads. And a periodic timer interrupt can be used to switch thread contexts. This is the first step towards a preemptive multitasking operating system.
Upvotes: 6