Advertisement
Guest User

lista + threads = pesadelos

a guest
May 30th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  5. pthread_cond_t vazia = PTHREAD_COND_INITIALIZER;
  6. pthread_cond_t cheia = PTHREAD_COND_INITIALIZER;
  7.  
  8. typedef struct elem{
  9.     int value;
  10.     struct elem *prox;
  11. }Elem;
  12.  
  13. typedef struct blockingQueue{
  14.     unsigned sizeBuffer, statusBuffer;
  15.     Elem *head, *last;
  16. }BlockingQueue;
  17.  
  18. BlockingQueue* newBlockingQueue(unsigned inSizeBuffer){
  19.     BlockingQueue* novo;
  20.     novo = (BlockingQueue*) malloc(1*(sizeof(BlockingQueue)));
  21.     novo->sizeBuffer = inSizeBuffer;
  22.     novo->statusBuffer = 0;
  23.     novo->head = novo->last = NULL;
  24.     return novo;
  25. }
  26. void putBlockingQueue(BlockingQueue* Q, int newValue){
  27.     Elem* novo;
  28.     Elem* iter;
  29.     if(Q->sizeBuffer==Q->statusBuffer){
  30.         printf("A fila esta cheia.\n");         //aqui ficaria a pausa na thread produtora e o despertar da consumidora
  31.     }
  32.     else if(Q->statusBuffer==0){
  33.         novo=(Elem*) malloc(1*sizeof(Elem));
  34.         Q->head=novo;
  35.         Q->last=novo;
  36.         novo->value=newValue;
  37.         novo->prox=NULL;
  38.     }
  39.     else{
  40.         novo=(Elem*) malloc(1*(sizeof(Elem)));
  41.         novo->value=newValue;
  42.         novo.prox=NULL;
  43.         Q->last=novo;
  44.     }
  45. }
  46. int takeBlockingQueue(BlockingQueue* Q){
  47.     int first;
  48.     Elem* iter;
  49.     if(Q->statusBuffer==0){
  50.         printf("A fila esta vazia.\n"); //aqui ficaria a pausa da thread consumidora e o despertar da produtora
  51.     }
  52.     else{
  53.         iter=Q->head;
  54.         first=iter->value;
  55.         Q->head=iter->prox;
  56.         return first;
  57.     }
  58. }
  59. int main(){
  60.    
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement