本帖最后由 adlu 于 2016-12-2 10:43 编辑
mbed开发中,Queue and MemoryPool example代码如下:
- #include "mbed.h"
- #include "rtos.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;
- /* Send Thread */
- void send_thread (void const *args) {
- 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);
- Thread::wait(1000);
- }
- }
- int main (void) {
- Thread thread(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);
- }
- }
- }
复制代码 请问第10~11行的代码,怎么理解?
MemoryPool<message_t, 16> mpool;
Queue<message_t, 16> queue;
是C++的什么语法?
|
在MemoryPool.h和Queue.h中对这两个类的定义时,包含了其模板。
template<typename T, uint32_t pool_sz>
class MemoryPool {...}
template<typename T, uint32_t queue_sz>
class Queue {...}
MemoryPool<message_t, 16> mpool;//创建一个MemoryPool对象,名称为mpool,存储类型为message_t,容量为16
Queue<message_t, 16> queue;//创建一个Queue对象,名称为queue,存储类型为message_t的指针,长度为16
C++中模板Template的使用,参考:http://blog.sina.com.cn/s/blog_74a271040100tkmj.html