Code:
/*=================Last Connected======================
* Copyright (C) 2005-2006 Brian "devicenull" Rak
* License: zlib/libpng
* $Id: safeStack.h 23 2006-06-16 18:33:22Z $
=====================================================*/
#include "sh_stack.h"
#include "pthread.h"
template <class T> class safeStack
{
public:
void init()
{
pthread_mutex_init(&mSafe,NULL);
}
void destroy()
{
pthread_mutex_lock(&mSafe);
pthread_mutex_destroy(&mSafe);
}
void push(T newItem)
{
pthread_mutex_lock(&mSafe);
sStack.push(newItem);
pthread_mutex_unlock(&mSafe);
}
T& pop()
{
pthread_mutex_lock(&mSafe);
T &temp = sStack.front();
sStack.pop();
pthread_mutex_unlock(&mSafe);
return temp;
}
int count()
{
pthread_mutex_lock(&mSafe);
int temp = sStack.size();
pthread_mutex_unlock(&mSafe);
return temp;
}
private:
SourceHook::CStack<T> sStack;
pthread_mutex_t mSafe;
};
My question: Is this a good way of doing it? I only need push, pop, and count, so those are the only one's I've implemented. I don't initalize the mutex in the constructor because I have to initalize the pthread library before I can call that, and I've set it up to not use a pointer to the safeStack object.
__________________