Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 typedef struct { 00003 int value; 00004 } message_t; 00005 const int size = 8; 00006 MemoryPool<message_t, size> mpool; 00007 Queue<message_t, size> queue; 00008 Thread thread1; 00009 Thread thread2; 00010 Mutex mutex; 00011 ConditionVariable cond(mutex); 00012 int numberOfElement = 0; 00013 00014 void produce() { 00015 while(true) { 00016 wait(rand()%5); 00017 int value = rand(); 00018 mutex.lock(); 00019 while(numberOfElement == size) { 00020 printf("queue is full"); 00021 cond.wait(); 00022 } 00023 message_t *message = mpool.alloc(); 00024 message->value = value; 00025 queue.put(message); 00026 numberOfElement++; 00027 printf("\nPut Message: %d\n\r" , message->value); 00028 cond.notify_all(); 00029 mutex.unlock(); 00030 } 00031 } 00032 00033 void consume() { 00034 while(true) { 00035 wait(rand()%5); 00036 mutex.lock(); 00037 while(numberOfElement == 0) { 00038 printf("queue is empty"); 00039 cond.wait(); 00040 } 00041 osEvent evt = queue.get(); 00042 numberOfElement--; 00043 if (evt.status == osEventMessage) { 00044 message_t *message = (message_t*)evt.value.p; 00045 printf("\nConsumed Message: %d\n\r" , message->value); 00046 mpool.free(message); 00047 } 00048 cond.notify_all(); 00049 mutex.unlock(); 00050 00051 } 00052 } 00053 int main() { 00054 thread1.start(callback(produce)); 00055 thread2.start(callback(consume)); 00056 thread1.join(); 00057 thread2.join(); 00058 }
Generated on Sat Aug 6 2022 09:53:47 by
1.7.2