我想获得关于下面列出的IService类的一些反馈。据我所知,这种类型与“活动对象”模式有关。如果我错误地使用任何相关术语,请原谅/纠正。基本上这个想法是,使用这个活动对象类的类需要提供一个控制某个事件循环的start和stop方法。这个事件循环可以通过while循环或boost asio等实现。线程相关的活动对象设计问题(C++ boost)
该类负责以非阻塞的方式启动一个新线程,以便事件可以在新线程中处理。它还必须处理所有清理相关的代码。我首先尝试了一种面向对象方法,其中子类负责覆盖方法来控制事件循环,但清理很麻烦:在析构函数调用stop方法时,在调用类没有手动调用停止方法。模板化的解决方案似乎是一个更加简洁:
template <typename T>
class IService : private boost::noncopyable
{
typedef boost::shared_ptr<boost::thread> thread_ptr;
public:
IService()
{
}
~IService()
{
/// try stop the service in case it's running
stop();
}
void start()
{
boost::mutex::scoped_lock lock(m_threadMutex);
if (m_pServiceThread && m_pServiceThread->joinable())
{
// already running
return;
}
m_pServiceThread = thread_ptr(new boost::thread(boost::bind(&IService::main, this)));
// need to wait for thread to start: else if destructor is called before thread has started
// Wait for condition to be signaled and then
// try timed wait since the application could deadlock if the thread never starts?
//if (m_startCondition.timed_wait(m_threadMutex, boost::posix_time::milliseconds(getServiceTimeoutMs())))
//{
//}
m_startCondition.wait(m_threadMutex);
// notify main to continue: it's blocked on the same condition var
m_startCondition.notify_one();
}
void stop()
{
// trigger the stopping of the event loop
m_serviceObject.stop();
if (m_pServiceThread)
{
if (m_pServiceThread->joinable())
{
m_pServiceThread->join();
}
// the service is stopped so we can reset the thread
m_pServiceThread.reset();
}
}
private:
/// entry point of thread
void main()
{
boost::mutex::scoped_lock lock(m_threadMutex);
// notify main thread that it can continue
m_startCondition.notify_one();
// Try Dummy wait to allow 1st thread to resume???
m_startCondition.wait(m_threadMutex);
// call template implementation of event loop
m_serviceObject.start();
}
/// Service thread
thread_ptr m_pServiceThread;
/// Thread mutex
mutable boost::mutex m_threadMutex;
/// Condition for signaling start of thread
boost::condition m_startCondition;
/// T must satisfy the implicit service interface and provide a start and a stop method
T m_serviceObject;
};
类可以使用如下:
class TestObject3
{
public:
TestObject3()
:m_work(m_ioService),
m_timer(m_ioService, boost::posix_time::milliseconds(200))
{
m_timer.async_wait(boost::bind(&TestObject3::doWork, this, boost::asio::placeholders::error));
}
void start()
{
// simple event loop
m_ioService.run();
}
void stop()
{
// signal end of event loop
m_ioService.stop();
}
void doWork(const boost::system::error_code& e)
{
// Do some work here
if (e != boost::asio::error::operation_aborted)
{
m_timer.expires_from_now(boost::posix_time::milliseconds(200));
m_timer.async_wait(boost::bind(&TestObject3::doWork, this, boost::asio::placeholders::error));
}
}
private:
boost::asio::io_service m_ioService;
boost::asio::io_service::work m_work;
boost::asio::deadline_timer m_timer;
};
现在我的具体问题:
1)是利用升压条件变量正确吗?这对我来说似乎有些破绽:我想等待线程启动,所以我等待条件变量。然后,一旦新的线程在main方法中启动,我再次等待相同的条件变量以允许初始线程继续。然后一旦初始线程的启动方法退出,新线程就可以继续。这个可以吗?
2)是否有任何情况下,线程不会被操作系统成功启动?我记得在某处可能会发生这种情况。如果可以的话,我宁愿在条件变量上进行定时等待(正如start方法中注释的那样)?我知道模板类无法正确地实现停止方法,即如果事件循环无法停止,代码将阻止连接(在停止或在析构函数中),但是我看不到这个。我猜这取决于该课程的用户,以确保启动和停止方法正确实施?
4)我将不胜感激任何其他设计错误,改进等?
谢谢!
1)经过大量的测试中使用的条件变量的优良似乎
2)这个问题还没有冒出了(还)
3)模板类: