Mbed OS 文档翻译 之 参考(API(RTOS(Queue)))

Queue

                                                                   

                                                                                     Queue 类层次结构

Queue 允许您将指向生产者线程的数据的指针排队到使用者线程。

                                                       Mbed OS 文档翻译 之 参考(API(RTOS(Queue)))_第1张图片

Queue 类参考

rtos::Queue< T, queue_sz > 类模板参考

公共成员函数
  Queue ()
  ~Queue ()
bool  empty () const
bool  full () const
osStatus  put (T *data, uint32_t millisec=0, uint8_t prio=0)
osEvent  get (uint32_t millisec=osWaitForever)

Queue 示例

Queue queue;

message_t *message;

queue.put(message);

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

Queue 和 MemoryPool 示例

此示例显示 Queue 和 MemoryPool 管理测量。

main.cpp                                                                                                                                                导入到 Mbed IDE

#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 mpool;
Queue 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);
        wait(1);
    }
}

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);
        }
    }
}

  • MemoryPool API 参考。

你可能感兴趣的:(Mbed,OS)