Skip to content

Condition variable #5064

@DuyTrandeLion

Description

@DuyTrandeLion

Question

I am working on mbed RTOS and I realized that there is no document about condition variable. Do you support or is there any document about how to implement condition variable in mbed os? I tried searching but there was almost no document about it.

Thank you very much.

Activity

geky

geky commented on Sep 11, 2017

@geky
Contributor

Good thing to notice

@c1728p9 put up a pr to add a ConditionalVariable class, though it ended up dropped: #3648

For most cases, condition variables can be replaced with the EventFlags class (here), although synchronization needs to be handled seperately. The documentation should be updated with the 5.6 release which is on the way, but for now there is just the doxygen in the header file.

Here's a simple example:

EventFlags flags;
Mutex mutex;
bool condition = false;

void wait_for_condition() {
    mutex.lock();
    while (!condition) {
        mutex.unlock();
        flags.wait_any(1);
        mutex.lock();
    }
    mutex.unlock();
}

void trigger_condition() {
    mutex.lock(); // note, we don't need this mutex if we are in interrupt context
    condition = true;
    flags.set(1);
    mutex.unlock();
}

Here's a real world example where we use event flags as condition variables in the socket classes:
https://github.com/ARMmbed/mbed-os/blob/master/features/netsocket/UDPSocket.cpp#L76

DuyTrandeLion

DuyTrandeLion commented on Sep 12, 2017

@DuyTrandeLion
Author

Thank you very much for you reply, I guess EventFlags is good as a replacement for ConditionalVariable. Still hope that one day ConditionalVariable would be reopened

kjbracey

kjbracey commented on Nov 14, 2017

@kjbracey
Contributor

#3648 has now been merged, so rtos::ConditionVariable is present on master, and will be in mbed-os-5.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @geky@0xc0170@kjbracey@DuyTrandeLion

        Issue actions

          Condition variable · Issue #5064 · ARMmbed/mbed-os