金庆的专栏

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  423 随笔 :: 0 文章 :: 454 评论 :: 0 Trackbacks

摘自: http://pocoproject.org/docs/00100-GuidedTour.html

Active object is an object that has methods executing in their own thread. This makes asynchronous member function calls possible — call a member function, while the function executes, do a bunch of other things, and, eventually, obtain the function's return value. The following example shows how this is done in POCO. The ActiveAdder class in defines an active method add() , implemented by the addImpl() member function. Invoking the active method in main() yields an Poco::ActiveResult (also known as a future), that eventually receives the function's return value.

#include "Poco/ActiveMethod.h"
#include "Poco/ActiveResult.h"
#include <utility>

using Poco::ActiveMethod;
using Poco::ActiveResult;

class ActiveAdder
{
public:
ActiveObject(): activeAdd(this, &ActiveAdder::add)
{
}

ActiveMethod<int, std::pair<int, int>, ActiveAdder> add;

private:
int addImpl(const std::pair<int, int>& args)
{
return args.first + args.second;
}
};

int main(int argc, char** argv)
{
ActiveAdder adder;

ActiveResult<int> sum = adder.add(std::make_pair(1, 2));
// do other things
sum.wait();
std::cout << sum.data() << std::endl;

return 0;
}

posted on 2010-06-22 14:21 金庆 阅读(587) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理