摘自: 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
using Poco::ActiveMethod;
using Poco::ActiveResult;
class ActiveAdder
{
public:
ActiveObject(): activeAdd(this, &ActiveAdder::add)
{
}
ActiveMethod, ActiveAdder> add;
private:
int addImpl(const std::pair& args)
{
return args.first + args.second;
}
};
int main(int argc, char** argv)
{
ActiveAdder adder;
ActiveResult sum = adder.add(std::make_pair(1, 2));
// do other things
sum.wait();
std::cout << sum.data() << std::endl;
return 0;
}
分享到:
2010-06-22 14:12
浏览 299
评论