Use a shared queue of X items - One thread adds items at random times - The other thread pulls at random times - They need to synchronize when the queue is full (producer) or empty (consumer)!

Committer:
vicara
Date:
Thu Nov 29 17:08:02 2018 +0000
Revision:
0:180517623995
Child:
1:58c5e61231be
Use a shared queue of X items; - One thread adds items at random times; - The other thread pulls at random times; - They need to synchronize when the; queue is full (producer) or empty; (consumer)!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vicara 0:180517623995 1 #include "mbed.h"
vicara 0:180517623995 2
vicara 0:180517623995 3 Queue<int, 10> queue;
vicara 0:180517623995 4
vicara 0:180517623995 5 int queue_size = 10;
vicara 0:180517623995 6 int end_idx = 0;
vicara 0:180517623995 7
vicara 0:180517623995 8 Mutex mutex;
vicara 0:180517623995 9 ConditionVariable cond(mutex);
vicara 0:180517623995 10 InterruptIn button(USER_BUTTON);
vicara 0:180517623995 11
vicara 0:180517623995 12
vicara 0:180517623995 13 bool is_button_pressed = false;
vicara 0:180517623995 14
vicara 0:180517623995 15 Thread t1;
vicara 0:180517623995 16 Thread t2;
vicara 0:180517623995 17 Thread t3;
vicara 0:180517623995 18
vicara 0:180517623995 19 void start_producer(){
vicara 0:180517623995 20 int value = 0;
vicara 0:180517623995 21 while (true)
vicara 0:180517623995 22 {
vicara 0:180517623995 23 if (end_idx < queue_size){
vicara 0:180517623995 24 mutex.lock();
vicara 0:180517623995 25 value = end_idx;
vicara 0:180517623995 26 queue.put(&value);
vicara 0:180517623995 27 printf("PRODUCER: Put value %d \n", end_idx);
vicara 0:180517623995 28 end_idx++;
vicara 0:180517623995 29 cond.notify_all();
vicara 0:180517623995 30 mutex.unlock();
vicara 0:180517623995 31 }
vicara 0:180517623995 32 wait(rand()%10);
vicara 0:180517623995 33 }
vicara 0:180517623995 34 }
vicara 0:180517623995 35
vicara 0:180517623995 36 void start_consumer(){
vicara 0:180517623995 37 while (true)
vicara 0:180517623995 38 {
vicara 0:180517623995 39 if (end_idx > 0){
vicara 0:180517623995 40 mutex.lock();
vicara 0:180517623995 41 osEvent evt = queue.get();
vicara 0:180517623995 42 if (evt.status == osEventMessage) {
vicara 0:180517623995 43 printf("CONSUMER: I got %d\n", *(int *) evt.value.p);
vicara 0:180517623995 44 }
vicara 0:180517623995 45 end_idx--;
vicara 0:180517623995 46 cond.notify_all();
vicara 0:180517623995 47 mutex.unlock();
vicara 0:180517623995 48 }
vicara 0:180517623995 49 wait(rand()%5);
vicara 0:180517623995 50 }
vicara 0:180517623995 51 }
vicara 0:180517623995 52
vicara 0:180517623995 53 void toggle (){
vicara 0:180517623995 54 is_button_pressed = true;
vicara 0:180517623995 55 }
vicara 0:180517623995 56
vicara 0:180517623995 57 void print_status(){
vicara 0:180517623995 58 while (true) {
vicara 0:180517623995 59 if(is_button_pressed){
vicara 0:180517623995 60 mutex.lock();
vicara 0:180517623995 61 wait(2);
vicara 0:180517623995 62 printf("\n\nThere are %d elements in the queue\n\n", end_idx);
vicara 0:180517623995 63 wait(2);
vicara 0:180517623995 64 mutex.unlock();
vicara 0:180517623995 65 is_button_pressed = false;
vicara 0:180517623995 66 }
vicara 0:180517623995 67 }
vicara 0:180517623995 68 }
vicara 0:180517623995 69
vicara 0:180517623995 70 int main() {
vicara 0:180517623995 71 button.rise(&toggle);
vicara 0:180517623995 72 t1.start(&start_producer);
vicara 0:180517623995 73 t2.start(&start_consumer);
vicara 0:180517623995 74 t3.start(&print_status);
vicara 0:180517623995 75 }