ACE_Condition
1.与POSIX下pthread_cond_wait()类似
/**
* @class ACE_Condition
*
* @brief ACE_Condition variable wrapper, which allows threads to block
* until shared data changes state.
*
* A condition variable enables threads to atomically block and
* test the condition under the protection of a mutual exclu-
* sion lock (mutex) until the condition is satisfied. That is,
* the mutex must have been held by the thread before calling
* wait or signal on the condition. If the condition is false,
* a thread blocks on a condition variable and atomically
* releases the mutex that is waiting for the condition to
* change. If another thread changes the condition, it may wake
* up waiting threads by signaling the associated condition
* variable. The waiting threads, upon awakening, reacquire the
* mutex and re-evaluate the condition.
* Note, you can only parameterize <ACE_Condition> with
* @a ACE_Thread_Mutex, @a ACE_Recursive_Thread_Mutex, or @a ACE_Null_Mutex.
*/
这个接口和POSIX下的pthread_cond_wait() 接口使用非常类似,都要与一个互斥锁一起配合使用,可以用来做线程池使用。没有找到ACE的实现代码,可能实现也使用了这个底层接口。
2.ACE_Condition_Thread_Mutex与ACE_Condition<ACE_Thread_Mutex> 作用一样,但类型不一样
使用ACE_Condition_Thread_Mutex定义的变量与ACE_Thread_Condition<Mutex>和ACE_Condition<ACE_Thread_Mutex>定义的变量都有共同的接口(类前面功能注释都一样),ACE_Thread_Condition继承自ACE_Condition。感觉有点乱,可能理解的不透彻,难道是为了达到一致性才这样设计的么。
3.使用Demo
// condition
#include "ace/Task.h"
#include "ace/Condition_T.h"
class HADeviceRespository
{
public:
HADeviceRespository() : m_owner(0){}
int isFree(void)
{
return (this->m_owner == 0);
}
int isOwner(ACE_Task_Base *tb)
{
return (this->m_owner == tb);
}
ACE_Task_Base *getOwner(void)
{
return this->m_owner;
}
void setOwner(ACE_Task_Base *owner)
{
this->m_owner = owner;
}
int updateDevice(int deviceId)
{
return 0;
}
private:
ACE_Task_Base *m_owner;
};
class HACommandHander : public ACE_Task_Base
{
public:
enum {numUSES = 10};
//HACommandHander(HADeviceRespository & rep, ACE_Condition<ACE_Thread_Mutex> & wait, ACE_Thread_Mutex * repMutex)
HACommandHander(HADeviceRespository & rep, ACE_Condition_Thread_Mutex & wait, ACE_Thread_Mutex * repMutex)
:m_rep(rep), m_wait(wait), m_repMutex(repMutex)
{}
virtual int svc (void)
{
for (int i=0; i<numUSES; i++)
{
ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%t) Hander Thread running\n")));
ACE_OS::sleep(1);
this->m_repMutex->acquire();
while (!this->m_rep.isFree())
{
this->m_wait.wait();
}
this->m_rep.setOwner(this);
this->m_repMutex->release();
this->m_rep.updateDevice(i);
ACE_ASSERT(this->m_rep.isOwner(this));
this->m_rep.setOwner(0);
this->m_wait.signal();
}
return 0;
}
private:
// we can only use ACE_Thread_Mutex *
ACE_Thread_Mutex * m_repMutex;
HADeviceRespository& m_rep;
//ACE_Condition<ACE_Thread_Mutex>& m_wait;
ACE_Condition_Thread_Mutex& m_wait;
};
int ACE_TMAIN(int, ACE_TCHAR *[])
{
HADeviceRespository rep;
ACE_Thread_Mutex repMutex;
//ACE_Condition<ACE_Thread_Mutex> wait(repMutex);
ACE_Condition_Thread_Mutex wait(repMutex);
HACommandHander handler1 (rep, wait, &repMutex);
HACommandHander handler2 (rep, wait, &repMutex);
handler1.activate();
handler2.activate();
handler1.wait();
handler2.wait();
return 0;
}