我正在尝试为自定义R / W Lock编写模板锁定。我希望它模板化,以便我可以使用防护装置进行不同的锁定,但我无法使其工作。我收到的错误如下:
Severity Code Description Project File Line
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int jimdbMSVS d:\...\src\thread\rwlockguard.hpp 6
Error C2588 '::~RWLockGuard': illegal global destructor jimdbMSVS d:\...\src\thread\rwlockguard.hpp 20
Error C2433 'RWLockGuard': 'inline' not permitted on data declarations jimdbMSVS d:\...\src\thread\rwlockguard.hpp 6
Error C2447 '{': missing function header (old-style formal list?) jimdbMSVS d:\...\src\thread\rwlockguard.hpp 21
Error C2059 syntax error: '<' jimdbMSVS d:\git_repo\jim-db\src\thread\rwlockguard.hpp 6
Error C2143 syntax error: missing ';' before '<' jimdbMSVS d:\...\src\thread\rwlockguard.hpp 6
Error C2143 syntax error: missing ';' before '{' jimdbMSVS d:\...\src\thread\rwlockguard.hpp 21
Error C2988 unrecognizable template declaration/definition jimdbMSVS d:\...\src\thread\rwlockguard.hpp 6
我知道我需要强制执行4种方法,但我不知道它是如何正确的,以及我如何使其工作。那么我做错了什么/对吧?
部首:
#pragma once
#include "rwlock.h"
namespace jimdb{ namespace tasking {
enum LockType
{
READ,
WRITE
};
template<typename T = RWLock>
class RWLockGuard{
public:
inline RWLockGuard(T& lock, const LockType& l);
inline ~RWLockGuard();
private:
T& m_lock;
LockType m_type;
};
}} // jimdb::tasking
#include "rwlockguard.hpp"
和hpp:
namespace jimdb{ namespace tasking {
template<typename T>
inline RWLockGuard<T>::RWLockGuard(T& lock, const LockType& l) : m_lock(lock), m_type(l)
{
switch(m_type)
{
case READ:
m_lock.readLock();
break;
case WRITE:
m_lock.writeLock();
break;
}
}
template<typename T>
inline RWLockGuard<T>::~RWLockGuard()
{
switch (m_type)
{
case READ:
m_lock.readUnlock();
break;
case WRITE:
m_lock.writeUnlock();
break;
}
}
}} // jimdb::tasking
rwlock.h
namespace jimdb{ namespace tasking {
class RWLock {
public:
inline RWLock();
inline void readLock();
inline void readUnlock();
inline void writeLock();
inline void writeUnlock();
private:
int m_reader;
bool m_writing;
std::condition_variable m_cond;
std::mutex m_mutex;
};
}} // jimdb::tasking
目标是:
RWLockGuard<RWLock> lock(mylock,READ);