Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- pthread_cond_t vazia = PTHREAD_COND_INITIALIZER;
- pthread_cond_t cheia = PTHREAD_COND_INITIALIZER;
- typedef struct elem{
- int value;
- struct elem *prox;
- }Elem;
- typedef struct blockingQueue{
- unsigned sizeBuffer, statusBuffer;
- Elem *head, *last;
- }BlockingQueue;
- BlockingQueue* newBlockingQueue(unsigned inSizeBuffer){
- BlockingQueue* novo;
- novo = (BlockingQueue*) malloc(1*(sizeof(BlockingQueue)));
- novo->sizeBuffer = inSizeBuffer;
- novo->statusBuffer = 0;
- novo->head = novo->last = NULL;
- return novo;
- }
- void putBlockingQueue(BlockingQueue* Q, int newValue){
- Elem* novo;
- Elem* iter;
- if(Q->sizeBuffer==Q->statusBuffer){
- printf("A fila esta cheia.\n"); //aqui ficaria a pausa na thread produtora e o despertar da consumidora
- }
- else if(Q->statusBuffer==0){
- novo=(Elem*) malloc(1*sizeof(Elem));
- Q->head=novo;
- Q->last=novo;
- novo->value=newValue;
- novo->prox=NULL;
- }
- else{
- novo=(Elem*) malloc(1*(sizeof(Elem)));
- novo->value=newValue;
- novo.prox=NULL;
- Q->last=novo;
- }
- }
- int takeBlockingQueue(BlockingQueue* Q){
- int first;
- Elem* iter;
- if(Q->statusBuffer==0){
- printf("A fila esta vazia.\n"); //aqui ficaria a pausa da thread consumidora e o despertar da produtora
- }
- else{
- iter=Q->head;
- first=iter->value;
- Q->head=iter->prox;
- return first;
- }
- }
- int main(){
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement