[SOLVED] std::mutex support

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • [SOLVED] std::mutex support

    Good day ladies and gentlemen,
    I have a little question about C++ support for Embedded Studio. I trying use std::mutex, but I'm getting an error: "no type named 'mutex' in namespace 'std'".
    This is a bit surprising since the header file <mutex> exists. Judging by its contents, the creation of a mutex is not available because multithreading is not available. It's strange, because even on a single-core controller, interrupts can occur, which means that mutexes are also needed in single-threaded mode.

    Do you have plans to add mutex support?

    example code:

    Source Code

    1. #include <mutex>
    2. int main (void) {
    3. std::mutex m_;
    4. for(;;){
    5. }
    6. }
    Thanks,
    Best regards
  • Hello,

    Thank you for your inquiry.

    CheMax wrote:

    again, without an RTOS I can't use the mutex from the standard library?
    That is correct. Same with other OS specific library functions. The standard library only defines what certain constructs should do when called. But how they operate is implementation defined by the RTOS that is being used.
    You can of course write your own multi tasking system, but then you also have to supply your setup specific mutex implementation.

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • CheMax wrote:

    because even on a single-core controller, interrupts can occur, which means that mutexes are also needed in single-threaded mode
    To implement a mutex that handles interrupts on a single-threaded system all you need to do is have the mutex disable interrupts when a mutex is owned and re-enable them when the mutex is freed. If the mutex must support nesting then a counter or similar must be incorporated so that interrupts are re-enabled only when the last mutex is freed.

    I don't know what hooks Segger's C++ library has for providing an implementation for the the std::mutex class, but I'd assume the docs have details (or if not someone from Segger could maybe chime in here).
  • SEGGER - Nino wrote:

    Hello,

    Thank you for your inquiry.

    CheMax wrote:

    again, without an RTOS I can't use the mutex from the standard library?
    That is correct. Same with other OS specific library functions. The standard library only defines what certain constructs should do when called. But how they operate is implementation defined by the RTOS that is being used.You can of course write your own multi tasking system, but then you also have to supply your setup specific mutex implementation.

    Best regards,
    Nino
    Hello.
    I was carried away by the project, I forgot to go to the forum. Thanks for your reply.

    The library is located in the system directory, visible in all projects. I need to make some changes to it, in particular, include header files.
    When updating a library (there is nothing better than updating something that works :) ) will my changes be erased?
  • I would think that Segger would have some supported way of customizing how the mutex is implemented, so that you could integrate it into an RTOS that is not supported out-of-the-box. But I'm not using Embedded Studio so I'm not sure what that mechanism is (or even if it actually exists - I'm just speculating).
  • mwb1100 wrote:

    I would think that Segger would have some supported way of customizing how the mutex is implemented, so that you could integrate it into an RTOS that is not supported out-of-the-box. But I'm not using Embedded Studio so I'm not sure what that mechanism is (or even if it actually exists - I'm just speculating).
    but what You using? :)