Mistake on this page?
Report an issue in GitHub or email us

Queue

Queue class hierarchy

A Queue allows you to queue pointers to data from producer threads to consumer threads.

Queue class reference

Public Member Functions
 Queue ()
 Create and initialize a message Queue of objects of the parameterized type T and maximum capacity specified by queue_sz. More...
 ~Queue ()
 Queue destructor. More...
bool empty () const
 Check if the queue is empty. More...
bool full () const
 Check if the queue is full. More...
uint32_t count () const
 Get number of queued messages in the queue. More...
bool try_put (T *data, uint8_t prio=0)
 Inserts the given element to the end of the queue. More...
bool try_put_for (Kernel::Clock::duration_u32 rel_time, T *data, uint8_t prio=0)
 Inserts the given element to the end of the queue. More...
osStatus put (T *data, uint32_t millisec=0, uint8_t prio=0)
 Inserts the given element to the end of the queue. More...
bool try_get (T **data_out)
 Get a message from the queue. More...
bool try_get_for (Kernel::Clock::duration_u32 rel_time, T **data_out)
 Get a message or wait for a message from the queue. More...
osEvent get (uint32_t millisec=osWaitForever)
 Get a message or wait for a message from the queue. More...

Queue example

Queue<message_t, 32> queue;

message_t *message;

queue.put(message);

osEvent evt = queue.get();
if (evt.status == osEventMessage) {
    message_t *message = (message_t*)evt.value.p;
}

Queue and MemoryPool example

This example shows Queue and MemoryPool managing measurements.

/*
 * Copyright (c) 2006-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */
#include "mbed.h"

typedef struct {
    float    voltage;   /* AD result of measured voltage */
    float    current;   /* AD result of measured current */
    uint32_t counter;   /* A counter value               */
} message_t;

MemoryPool<message_t, 16> mpool;
Queue<message_t, 16> queue;
Thread thread;

/* Send Thread */
void send_thread(void)
{
    uint32_t i = 0;
    while (true) {
        i++; // fake data update
        message_t *message = mpool.alloc();
        message->voltage = (i * 0.1) * 33;
        message->current = (i * 0.1) * 11;
        message->counter = i;
        queue.put(message);
        ThisThread::sleep_for(1000);
    }
}

int main(void)
{
    thread.start(callback(send_thread));

    while (true) {
        osEvent evt = queue.get();
        if (evt.status == osEventMessage) {
            message_t *message = (message_t *)evt.value.p;
            printf("\nVoltage: %.2f V\n\r", message->voltage);
            printf("Current: %.2f A\n\r", message->current);
            printf("Number of cycles: %u\n\r", message->counter);

            mpool.free(message);
        }
    }
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.