Advertisement
Guest User

TVH Queue Example

a guest
Nov 20th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "queue.h"
  5.  
  6. /* Struct containing entries to go in the queue
  7.  * TODO: add you're fields
  8.  * TODO: name sensibly
  9.  */
  10. typedef struct queue_entry
  11. {
  12.   TAILQ_ENTRY(queue_entry) link;
  13.   // Add working fields
  14.   int data;
  15. } queue_entry_t; // Name appropriately
  16.  
  17. /* Queue, Cond to signal data and Mutex to protect it */
  18. static TAILQ_HEAD(,queue_entry) queue;
  19. static pthread_mutex_t          mutex;
  20. static pthread_cond_t           cond;
  21.  
  22. /*
  23.  * Basic pattern for a queue driven thread
  24.  */
  25. void *a_thread ( void *aux )
  26. {
  27.   queue_entry_t *qe;
  28.   pthread_mutex_lock(&mutex);
  29.  
  30.   while (1) { // do you need an exit condition - probably not
  31.    
  32.     /* Get entry from queue */
  33.     qe = TAILQ_FIRST(&queue);
  34.     if (!qe) { // Queue empty
  35.       pthread_cond_wait(&cond, &mutex); // Note: this will unlock mutex
  36.                                         //       on entry and lock on exit
  37.       continue;                         // Go back to get entry (or if exit)
  38.     }
  39.     TAILQ_REMOVE(&queue, qe, link);     // Remove entry and we're done with Q
  40.     pthread_mutex_unlock(&mutex);       // now other threads can add while
  41.                                         // we process
  42.  
  43.     // TOOD: DO SOME WORK
  44.     printf("DATA: %d\n", qe->data);
  45.   }
  46.    
  47.   // Cannot reach here unless you have exit condition
  48.   return NULL;
  49. }
  50.  
  51. /*
  52.  * Initialise
  53.  */
  54. void init ( void )
  55. {
  56.   pthread_t tid; // you probably don't need to keep this
  57.  
  58.   pthread_mutex_init(&mutex, NULL); // Use default config
  59.   pthread_cond_init(&cond, NULL);   // ditto
  60.  
  61.   /* Start thread - presumably permanently active */
  62.   pthread_create(&tid, NULL, a_thread, NULL); // last param is passed as aux
  63.                                               // as this is single global
  64.                                               // you can probably use global
  65.                                               // vars
  66. }
  67.  
  68. /*
  69.  * Add data to the queue
  70.  *
  71.  * TODO: you can either have user pass in just the "data"
  72.  *       or a full queue_entry_t but you'd need to define struct in header
  73.  */
  74. void add ( int data )
  75. {
  76.   /* Create entry */
  77.   queue_entry_t *qe = calloc(1, sizeof(queue_entry_t));
  78.   qe->data = data;
  79.  
  80.   /* Insert */
  81.   pthread_mutex_lock(&mutex);
  82.   TAILQ_INSERT_TAIL(&queue, qe, link);
  83.   pthread_cond_signal(&cond); // tell thread data is available
  84.   pthread_mutex_unlock(&mutex);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement