Kinda vague title, but I'll try to explain below:
I have two interfaces, IMutex and IConditionVariable, and behind them the concrete implementations.
These are created/destroyed by a factory class.
IMutex looks like this:
class IMutex
{
public:
enum MutexState
{
UNLOCKED = 0,
LOCKED
};
virtual ~IMutex() { }
virtual int32_t Lock() = 0;
virtual int32_t Unlock() = 0;
virtual const MutexState& GetMutexState() const = 0;
};
IConditionVariable looks like this:
class IConditionVariable
{
public:
enum ConditionVariableState
{
READY,
WAITING
};
virtual ~IConditionVariable() { }
virtual void Wait(IMutex* mutex) = 0;
virtual void TimedWait(IMutex* mutex, uint32_t milliseconds) = 0;
virtual void Signal() = 0;
virtual void Broadcast() = 0;
virtual const ConditionVariableState& GetConditionVariableState() const = 0;
};
Two question:
1.
In the implementation of condition variable, I need to have the native mutex handle behind the IMutex so I can pass it to pthreads/windows api. But currently there is no way to aquire it.
To this I have two solutions:
- Make IConditionVariable a friend of IMutex, typedef the native handle and make it a protected member variable. Don't like this one very much, they shouldn't be coupled?
- Add a method to IMutex "GetNativeHandle" which returns the underlying platform handle. Also requries me to typedef the native handle. I like this one better, but I'd rather encapsulate the handle.
I prefer the second option, but is there any other way?
2,
In both cases above, I need to typedef the native handle in the interface, either to return it or to declare it as a protected member
#if defined _WIN32 || _WIN64
typedef CRITICAL_SECTION MutexHandle;
#else
typedef pthread_mutex_t MutexHandle;
#endif
But, for windows I need the definition of CRITICAL_SECTION. The only solution I have found is to include the <windows.h> with WIN32_LEAN_AND_MEAN... but I'd rather not have to include the windows header in an interface file.
How can I solve this one?