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 Mutex mutex; 00003 ConditionVariable cond(mutex); 00004 const int size = 3; 00005 int numberOfItems = 0; 00006 00007 typedef struct{ 00008 int number; 00009 }message_t; 00010 MemoryPool<message_t, size> mpool; 00011 Queue<message_t, size> queue; 00012 00013 void consumer(){ 00014 while(true){ 00015 wait(rand() % 3); 00016 mutex.lock(); 00017 if(numberOfItems > 0){ 00018 osEvent evt = queue.get(); 00019 message_t *message = (message_t*)evt.value.p; 00020 printf("number %d consumed", message->number); 00021 numberOfItems--; 00022 printf("Consumer. there are %d elements in queue\n", numberOfItems); 00023 mpool.free(message); 00024 }else{ 00025 printf("Consumer. queue is empty\n"); 00026 } 00027 cond.notify_all(); 00028 mutex.unlock(); 00029 } 00030 } 00031 00032 00033 int main() { 00034 Thread thread; 00035 thread.start(consumer); 00036 00037 //Producer 00038 while(true){ 00039 wait(rand() % 3); 00040 mutex.lock(); 00041 if(numberOfItems < size){ 00042 message_t *message = mpool.alloc(); 00043 message->number = rand(); 00044 queue.put(message); 00045 numberOfItems++; 00046 printf("Producer. there are %d elements in queue\n", numberOfItems); 00047 }else{ 00048 printf("Producer. queue full\n"); 00049 } 00050 cond.notify_all(); 00051 mutex.unlock(); 00052 } 00053 }
Generated on Fri Jul 15 2022 01:55:32 by
1.7.2