File synch.cc.
File threadtest.cc with our code.
We don't have any error after compile.
Screenshot of Producer-Consumer execution with semaphores.
Screenshot of Producer-Consumer execution with locks.
Some code:
Lock * L = new Lock("Lock"); void ConsumerLock(int which) { while (1) { if (disponibles > 0) { L->Acquire(); disponibles--; printf("Thread %d >> Consumidor >> disponibles %d\n", which, disponibles); L->Release(); } if (disponibles == 0) { currentThread->Finish(); } currentThread->Yield(); } }
void ProducerLock(int which) { while (1) { if (disponibles < 15) { L->Acquire(); disponibles++; printf("Thread %d >> Productor >> disponibles %d\n", which, disponibles); L->Release(); } if (disponibles == 15) { currentThread->Finish(); } currentThread->Yield(); } }
void ThreadTestLocks() { DEBUG('t', "Entra al ThreadTestLocks"); printf("Locks\n"); Thread *t = new Thread("Consumer"); t->Fork((VoidFunctionPtr)ConsumerLock, (void *)1); Thread *tr = new Thread("Productor"); tr->Fork((VoidFunctionPtr)ProducerLock, (void *)1); ProducerLock(0); }