-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue_memorypool.cpp
71 lines (55 loc) · 1.64 KB
/
queue_memorypool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//This is in the style of the offical Mbed example
//The queue has pointers to your type
//You have memory somewhere to store values (eg memorypool or could also be locations in an array)
//And the queue will send pointers to these values
//However, when you use try_get, you must pass a double pointer
#include <mbed.h>
Thread get_thread(osPriorityNormal, OS_STACK_SIZE, nullptr, "get thread");
Thread put_thread(osPriorityNormal, OS_STACK_SIZE, nullptr, "put thread");
Queue<int, 8> q1;
MemoryPool<int, 8> mp;
void put_loop()
{
int count = 0;
while(true)
{
int* mp_slot_ptr = mp.try_alloc_for(Kernel::wait_for_u32_forever);
*mp_slot_ptr = count;
bool put_status = q1.try_put(mp_slot_ptr);
if(!put_status)
{
printf("Queue full at size %d\n", q1.count());
}
count++;
ThisThread::sleep_for(1s);
}
}
void get_loop()
{
//"The message is stored in the location pointed to be the parameter data_out"
int* data_in;
//Yes, we provide a pointer to a pointer so they know where to store the pointer
int** queue_received = &data_in;
while(true)
{
bool get_status = q1.try_get(queue_received);
if(get_status)
{
printf("\nGet loop got: %d\n", *data_in);
printf("Mempool free: %d\n", mp.free(data_in));
//OR:
//printf("Get loop got: %d\n", *queue_received);
}
ThisThread::sleep_for(1s);
}
}
int main()
{
printf("Queue example\n");
put_thread.start(put_loop);
get_thread.start(get_loop);
while(true)
{
ThisThread::sleep_for(1s);
}
}