我想要一些关于下面列出的iService类的反馈。据我所知,这种类型的类与“活动对象”模式相关。如果我使用任何相关术语不正确,请原谅/纠正。基本上,使用这个活动对象类的类需要提供一个开始和停止方法来控制一些事件循环。这个事件循环可以用while循环或boost asio等实现。
这个类负责以非阻塞方式启动一个新线程,以便新线程可以处理事件。它还必须处理所有与清理相关的代码。我首先尝试了一种OO方法,在这种方法中,子类负责重写方法来控制事件循环,但清理是混乱的:在调用stop方法的析构函数中,在调用类没有手动调用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)增压条件变量的使用是否正确?在我看来,这有点像一个黑客:我想等待线程被启动,所以我在条件变量上等待。然后,在主方法中启动新线程后,我再次等待相同的条件变量,以允许初始线程继续。然后,一旦退出初始线程的start方法,新线程就可以继续。这样行吗?
2)是否存在操作系统无法成功启动线程的情况?我记得在某个地方读到过这样的事。如果可能的话,我应该对条件变量进行定时等待(就像start方法中注释的那样)?
3)我知道模板类不能“正确地”实现stop方法,即如果事件循环停止失败,代码将在连接上阻塞(在stop或析构函数中),但我看不到解决这一问题的方法。我想应该由类的用户来确保start和stop方法的正确实现?
4)我会感激任何其他设计错误、改进等?
谢谢!