Updates from lab.

This commit is contained in:
shadow8t4 2015-11-06 10:56:20 -06:00
parent 536fdb4aae
commit 8d38960b3f

View file

@ -53,7 +53,11 @@ public:
/* -- CONSTRUCTOR/DESTRUCTOR */
Semaphore(int _val){value = _val;}
Semaphore(int _val){
value = _val;
pthread_mutex_init(&m);
pthread_cond_init(&c);
}
~Semaphore(){}
@ -63,8 +67,11 @@ public:
//This is to lock the Semaphore.
pthread_mutex_lock( &m );
//Wait until condition, basically wait until signaled.
--value;
//If counter is negative, wait until condition, basically wait until signaled.
if(value < 0) {
pthread_cond_wait( &c, &m );
}
//Some code here.
@ -72,7 +79,17 @@ public:
pthread_mutex_unlock( &m );
}
//;
int V();
int V() {
pthread_mutex_lock( &m );
++value;
if(value <= 0) {
pthread_cond_signal( &c );
}
pthread_mutex_unlock( &m );
}
};
/*