- 论坛徽章:
- 0
|
本帖最后由 boyshell 于 2012-08-11 14:50 编辑
今天将代码从mac下搬到linux下,碰到个问题
这个是cpp- #include "Mutex.h"
- #ifdef PTHREAD_MUTEX_RECURSIVE_NP
- #define recursive_mutex_flag PTHREAD_MUTEX_RECURSIVE_NP
- #endif
- #ifdef PTHREAD_MUTEX_RECURSIVE
- #define recursive_mutex_flag PTHREAD_MUTEX_RECURSIVE
- #endif
- bool Mutex::attr_initalized = false;
- pthread_mutexattr_t Mutex::attr;
- Mutex::Mutex()
- {
- if(!attr_initalized)
- {
- pthread_mutexattr_init(&attr);
- pthread_mutexattr_settype(&attr, recursive_mutex_flag);
- attr_initalized = true;
- }
- pthread_mutex_init(&mutex, &attr);
- }
- Mutex::~Mutex() { pthread_mutex_destroy(&mutex); }
- bool Mutex::AttemptAcquire()
- {
- return (pthread_mutex_trylock(&mutex) == 0);
- }
- void Mutex::Acquire()
- {
- pthread_mutex_lock(&mutex);
- }
- void Mutex::Release()
- {
- pthread_mutex_unlock(&mutex);
- }
复制代码 这个是h- #ifndef _MUTEX_H_
- #define _MUTEX_H_
- #include <pthread.h>
- class Mutex
- {
- public:
- Mutex();
- virtual ~Mutex();
- bool AttemptAcquire();
- void Acquire();
- void Release();
- protected:
- static bool attr_initalized;
- static pthread_mutexattr_t attr;
- pthread_mutex_t mutex;
- };
- #define FastMutex Mutex
- #endif
复制代码 编译的时候出现了如下错误
[shell@shell lib]$ make
g++ -g -Wall -O3 -fPIC -shared -I. -MMD -c -o Mutex.o Mutex.cpp
Mutex.cpp: In constructor ‘Mutex::Mutex()’:
Mutex.cpp:18: 错误:‘recursive_mutex_flag’在此作用域中尚未声明
make: *** [Mutex.o] 错误 1
|
|