I'm getting weird errors for a semaphore class i'm writing. I don't know
what's causing this and have searched all over google for the answer with no luck.
Here's the code. please help! thanks!
Code:
// semaphore.hpp
#include <pthread.h>
#include <stdio.h>
#include <cstdlib>
#include <unistd.h>
#ifndef __SEMAPHORE_HPP_
#define __SEMAPHORE_HPP_
namespace Threading {
class Semaphore {
public:
Semaphore(unsigned int resource=0);
/* The initial value for the semaphore can be specified */
~Semaphore() { /* nothing to do */ }
int getCount() const { return sCount; }
/* Get the current value of a semaphore. */
void post();
/* Posting to a semaphore increments its current value
* and releases the first thread waiting for the semaphore
* if it is currently at 0. */
bool tryWait();
/* TryWait is a non-blocking variant of Wait. */
void wait();
/* Wait is used to keep a thread held until the semaphore
* counter is greater than 0. */
private:
int sCount;
/* Count of semaphore */
pthread_mutex_t mMutex;
/* Mutex to protect count */
pthread_mutex_t cMutex;
/* Mutex to protect condition value*/
pthread_cond_t condition;
/* Condition of availability of resource */
bool cValue;
/* Condition value */
};
};
#endif //__SEMAPHORE_HPP_
Code:
// semaphore.cpp
#include "semaphore.hpp"
using namespace Threading;
Semaphore::Semaphore(unsigned int resource) {
sCount = resource;
mMutex = PTHREAD_MUTEX_INITIALIZER;
cMutex = PTHREAD_MUTEX_INITIALIZER;
condition = PTHREAD_COND_INITIALIZER;
}
void Semaphore::post() {
pthread_mutex_lock(&mMutex);
sCount += 1;
if(sCount <= 0) {
// condition signal
pthread_mutex_lock(&cMutex);
cValue = false;
pthread_cond_signal(&condition);
pthread_mutex_unlock(&cMutex);
}
pthread_mutex_unlock(&mMutex);
}
bool Semaphore::tryWait() {
pthread_mutex_lock(&mMutex);
if(sCount <= 0) {
pthread_mutex_unlock(&mMutex);
return false;
}
sCount -= 1;
if(sCount < 0) {
pthread_mutex_unlock(&mMutex);
// condition wait
pthread_mutex_lock(&cMutex);
while(!cValue) {
pthread_cond_wait(&condition, &cMutex);
}
pthread_mutex_unlock(&cMutex);
// condition reset
pthread_mutex_lock(&cMutex);
cValue = false;
pthread_mutex_unlock(&cMutex);
}
else pthread_mutex_unlock(&mMutex);
return true;
}
void Semaphore::wait() {
if(sCount < 0) {
pthread_mutex_unlock(&mMutex);
// condition wait
pthread_mutex_lock(&cMutex);
while(!cValue) {
pthread_cond_wait(&condition, &cMutex);
}
pthread_mutex_unlock(&cMutex);
// condition reset
pthread_mutex_lock(&cMutex);
cValue = false;
pthread_mutex_unlock(&cMutex);
}
else pthread_mutex_unlock(&mMutex);
}
Code:
semaphore.cpp:7: parse error before `{'
semaphore.cpp: At top level:
semaphore.cpp:8: ISO C++ forbids declaration of `cMutex' with no type
semaphore.cpp:8: initializer for scalar variable requires one element
semaphore.cpp:9: ISO C++ forbids declaration of `condition' with no
type
semaphore.cpp:9: initializer for scalar variable requires one element
semaphore.cpp:10: parse error before `}'
make: *** [semaphore.o] Error 1
make: Target `proj3B' not remade because of errors.
Compilation exited abnormally with code 2 at Thu Nov 6 19:11:07