Advertisement
Guest User

C++ 11 MessageQueue Producer/Consumer GameProg.it

a guest
Apr 27th, 2013
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.77 KB | None | 0 0
  1. /**
  2. Copyright (c) 2013 Oliveri Dario
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10.  
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13.  
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21.  
  22. */
  23.  
  24. #include <mutex>
  25. #include <iostream>
  26.  
  27. /** Semplice sistema di loggin per fare debug e vedere cosa viene stampato.
  28. *   Un mutex globale è utilizzato per scrivere messaggi su console (ricordo
  29. *   che la console è una risorsa e ogni risorsa va protetta da un mutex in
  30. *   presenza di processi paralleli che la usano).*/
  31.  
  32. #if 0  //E' possibile cambiare 0 con 1 per disattivare i messaggi di debug.
  33.     #define DEBUG_LOG(X) (void(0))
  34. #else
  35.     std::mutex  GlobalMutex;
  36.  
  37.     #define DEBUG_LOG(x) \
  38.         GlobalMutex.lock(); \
  39.         std::cout<<x<<std::endl; \
  40.         GlobalMutex.unlock();
  41. #endif
  42.  
  43. typedef int  ProducerMessage;
  44.  
  45. /*******************************************************************************
  46. *   SharedBuffer, risorsa condivisa tra 2 thread per potersi scambiare messaggi.
  47. *   Non è vietato utilizzarla per più di 2 thread, ma solitamente è più
  48. *   conveniente utilizzare un buffer per ogni canale di comunicazione piuttosto
  49. *   che utilizzare 1 buffer per tutte le comunicazioni.
  50. *******************************************************************************/
  51. struct SharedBuffer{
  52.     std::mutex      casello;
  53.     ProducerMessage messages[8];
  54.  
  55.     // possiamo inizializzare qui e risparmiarci il costruttore.
  56.     int             separator = 0;
  57.     int             reader = 0;
  58.     int             writer = 0;
  59.  
  60.     //ritorna true se il messaggio è stato scritto in out_m
  61.     bool readMessage(ProducerMessage & out_m){
  62.         casello.lock(); //entro nel "casello autostradale"
  63.  
  64.         if(separator == 0){
  65.             //DEBUG_LOG("No message to read!");
  66.             casello.unlock(); //esco dal "casello"
  67.             return false;
  68.         }
  69.  
  70.         out_m = messages[reader]; //copio messaggio su out_m
  71.         reader++; //aggiorno posizione del reader
  72.         if(reader > 7)
  73.             reader=0;
  74.  
  75.         separator --;
  76.  
  77.         casello.unlock(); //esco dal "casello"
  78.         return true;
  79.     }
  80.  
  81.     //ritorna true se il messaggio è stato scritto dentro il buffer
  82.     bool writeMessage(ProducerMessage in){
  83.         casello.lock(); //entro nel casello
  84.  
  85.         if(separator==7){
  86.             //DEBUG_LOG("no empty space left");
  87.             casello.unlock();
  88.             return false;
  89.         }
  90.  
  91.         messages[writer]=in; //scrivo il messaggio dentro al buffer
  92.         writer++;
  93.         if(writer > 7)
  94.             writer=0;
  95.  
  96.         separator++;
  97.  
  98.         casello.unlock();
  99.         return true; //esco dal casello
  100.     }
  101. };
  102.  
  103. #include <cstdlib> //serve per "rand()".
  104. class Producer{
  105.  
  106.     SharedBuffer * buffer;
  107. public:
  108.  
  109.     /** Costruttore */
  110.     Producer(SharedBuffer * abuffer):
  111.         buffer(abuffer){   }
  112.  
  113.     void run(){
  114.         for(int i=0; i<16; i++){
  115.             int r = std::rand()%2; //numero casuale ritorna 0 oppure 1.
  116.             ProducerMessage mess = r;
  117.             switch(r){
  118.                 case 0:
  119.                     DEBUG_LOG("producer -> 0"); break;
  120.                 case 1:
  121.                     DEBUG_LOG("producer -> 1"); break;
  122.             }
  123.             //Attesa attiva che si liberi il buffer dei messaggi. In un
  124.             //codice reale si può imporre un limite alla durata dell'attesa
  125.             while(!buffer->writeMessage(mess)){}
  126.         }
  127.  
  128.         // Questo messaggio chiude il consumer.
  129.         DEBUG_LOG("producer -> 5");
  130.         while(!buffer->writeMessage(5)){}
  131.  
  132.     }
  133. };
  134.  
  135. class Consumer{
  136.  
  137.     SharedBuffer * buffer;
  138. public:
  139.  
  140.     /** Costruttore*/
  141.     Consumer(SharedBuffer * abuffer):
  142.         buffer(abuffer){  }
  143.  
  144.     void run(){
  145.         ProducerMessage mess;
  146.         while(mess!=5){
  147.             if(buffer->readMessage(mess)){
  148.               switch(mess){
  149.                 case 0:
  150.                     DEBUG_LOG("consumed 0!"); break;
  151.                 case 1:
  152.                     DEBUG_LOG("consumed 1!"); break;
  153.               }
  154.             }
  155.         }
  156.         DEBUG_LOG("consumed Quit");
  157.     }
  158. };
  159.  
  160.  
  161. #include <thread>
  162. int main(){
  163.     SharedBuffer buffer;
  164.     Producer B(&buffer);
  165.     Consumer C(&buffer);
  166.     std::thread consumerThread(&Consumer::run,&C);
  167.     std::thread producerThread(&Producer::run,&B);
  168.     consumerThread.join();
  169.     producerThread.join();
  170.     DEBUG_LOG("THE END");
  171.     return 0;
  172. }
  173.  
  174. /** OUTPUT EXAMPLE
  175. producer -> 1
  176. producer -> 1
  177. producer -> 0
  178. producer -> 0
  179. producer -> 1
  180. producer -> 0
  181. producer -> 0
  182. producer -> 0
  183. consumed 1!
  184. producer -> 0
  185. consumed 1!
  186. producer -> 0
  187. consumed 0!
  188. producer -> 1
  189. consumed 0!
  190. producer -> 1
  191. consumed 1!
  192. producer -> 1
  193. consumed 0!
  194. consumed 0!
  195. consumed 0!
  196. consumed 0!
  197. consumed 0!
  198. consumed 1!
  199. consumed 1!
  200. consumed 1!
  201. producer -> 1
  202. consumed 1!
  203. producer -> 1
  204. producer -> 1
  205. consumed 1!
  206. producer -> 5
  207. consumed 1!
  208. consumed Quit
  209. THE END
  210. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement