ACE学习(六)ACE_Condition

本文详细介绍了ACE库中ACE_Condition的使用方法及其与POSIX标准下的pthread_cond_wait()接口的相似之处。通过示例代码展示了如何利用ACE_Condition进行线程同步,包括条件变量的等待和信号操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值