- 论坛徽章:
- 0
|
gcc 为何union semun无法确定大小
#ifndef _APP_SEM_MUTEX_H__
#define _APP_SEM_MUTEX_H__
#include <pthread.h>;
#include <semaphore.h>;
#ifdef WIN32
#pragma comment(lib,"pthreadvc2.lib"
#endif
class CSem
{
public:
CSem(int init_val=1)
{
sem_init(&m_sem,0,init_val);
}
~CSem()
{
sem_destroy(&m_sem);
}
int wait()
{
return sem_wait(&m_sem);
}
//ret =0 ok else fail
int trywait()
{
return sem_trywait(&m_sem);
}
int post()
{
return sem_post(&m_sem);
}
private:
sem_t m_sem;
};
class CMutex
{
public:
CMutex()
{
pthread_mutexattr_init(&mta);
pthread_mutexattr_settype(&mta,PTHREAD_MUTEX_RECURSIVE_NP);
pthread_mutex_init(&mtx, &mta);
}
~CMutex()
{
pthread_mutexattr_destroy(&mta);
pthread_mutex_destroy(&mtx);
}
void lock()
{
pthread_mutex_lock(&mtx);
}
//ret =0 ok else fail
int trylock()
{
return pthread_mutex_trylock(&mtx);
}
void unlock()
{
pthread_mutex_unlock(&mtx);
}
private:
pthread_mutex_t mtx;
pthread_mutexattr_t mta;
};
#endif |
|