ACE学习(六)ACE_Condition

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


 

ace_wfmo_reactor 是 ACE(Adaptive Communication Environment)库中的一个反应器模式实现,用于处理并发事件。ACE 库是一个用于开发高性能网络应用程序的 C++ 库,提供了丰富的工具和模式。 在 ace_wfmo_reactor 中,加锁机制用于保护共享资源,防止多个线程同时访问同一资源导致的数据竞争和不一致性问题。以下是一些常见的加锁机制: 1. **互斥锁(Mutex)**:互斥锁是最基本的锁机制,用于确保同一时间只有一个线程可以访问共享资源。ACE 提供了 ACE_Mutex 类来实现互斥锁。 2. **读写锁(Read-Write Lock)**:读写锁允许多个线程同时读取共享资源,但写操作需要独占访问。ACE 提供了 ACE_RW_Thread_Mutex 类来实现读写锁。 3. **条件变量(Condition Variable)**:条件变量用于线程间的同步,允许线程在特定条件满足时等待或通知其他线程。ACE 提供了 ACE_Condition 类来实现条件变量。 以下是一个简单的示例,展示了如何在 ace_wfmo_reactor 中使用互斥锁保护共享资源: ```cpp #include <ace/Reactor.h> #include <ace/Mutex.h> #include <ace/Thread_Mutex.h> #include <iostream> class MyReactor : public ACE_Reactor { public: MyReactor() : shared_resource_(0) {} void increment_resource() { ACE_Guard<ACE_Mutex> guard(lock_); ++shared_resource_; std::cout << "Resource incremented to: " << shared_resource_ << std::endl; } void decrement_resource() { ACE_Guard<ACE_Mutex> guard(lock_); --shared_resource_; std::cout << "Resource decremented to: " << shared_resource_ << std::endl; } private: ACE_Mutex lock_; int shared_resource_; }; int main() { MyReactor reactor; reactor.increment_resource(); reactor.decrement_resource(); return 0; } ``` 在这个示例中,`MyReactor` 类继承自 `ACE_Reactor`,并包含一个共享资源 `shared_resource_`。`increment_resource` 和 `decrement_resource` 方法使用 `ACE_Guard` 和 `ACE_Mutex` 来确保对共享资源的访问是线程安全的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值