FreeRTOS and std smart pointers

georgbat wrote on Thursday, November 16, 2017:

Hello.
I have a simple question - is it safe to use std::shared_ptr with FreeRTOS multithread? Compiler is arm-gcc-none-eabi.

Thanks.

richard_damon wrote on Thursday, November 16, 2017:

std::shared_ptr shouldn’t have a problem. The one base issue that will need to be handled is that to use it you first will have needed to make sure that new/delete, and by normal implication malloc/free in the implementation have been made thread safe. Some libraries have some hooks that can be defined to interface with the OS to provied this ability. A generic implementation without such a hook is likely NOT thread safe.

Note, this is a library question, not a compiler issue, especially with GCC, as there are several different libraries that might be bundled with it for a given implementation.

georgbat wrote on Friday, November 17, 2017:

Thank you, much!

richard_damon wrote on Friday, November 17, 2017:

Thinking about this a bit, let me amend this a bit. I will presume you understand the general need to protect ANY cross task access to an object. If a given object is only referenced by smart pointers from only 1 task, this won’t be an issue. If you do pass an object between tasks with a smart pointer, than even though you might end up with multiple objects as smart pointers, they will share some internal state and thus they need protection. A typical smart pointer implementation should be using atomics for the important accesses, Atomics may or may not need support from the OS, so if you will be sharing smart pointers across tasks you should look into this.