Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This code shows how to perform a async/wait-for-result method, with ACE Activation Queues.
- Works on windows.
- */
- #include <stdio.h>
- #include <ace/Task.h>
- #include <ace/Activation_Queue.h>
- #include <ace/Method_Request.h>
- #include <ace/Future.h>
- #include <iostream>
- #include <string>
- using namespace std;
- class Executor;
- typedef ACE_Future<int> future_result;
- struct MethodProxy : public ACE_Method_Request
- {
- //friend Executor;
- int call() { return 0; }
- future_result * future_;
- public:
- MethodProxy() : future_(NULL) {}
- virtual int call(Executor*)=0;
- };
- struct Play : public MethodProxy
- {
- string filename_;
- Play(string filename, future_result * f=NULL) : filename_(filename) {
- future_=f;
- }
- int call(Executor * thiz_) {
- cout << "Playing " << filename_ << endl;
- Sleep(1000);
- return 666;
- }
- };
- class Executor : public ACE_Task_Base
- {
- ACE_Activation_Queue queue_;
- int svc()
- {
- int rc=0;
- ACE_Method_Request * method=NULL;
- MethodProxy * mp=NULL;
- while(true)
- {
- mp=(MethodProxy*)queue_.dequeue();
- if(NULL==mp) continue;
- rc=mp->call(this);
- if(mp->future_)
- mp->future_->set(rc);
- delete mp; // we are responsible for MethodProxy objects, but not for futures!
- }
- }
- public:
- int enqueue(MethodProxy * method)
- {
- return queue_.enqueue(method);
- }
- };
- int main(int argc, char* argv[])
- {
- Executor exec;
- exec.activate();
- future_result * future = new future_result;
- Play *play = new Play("c:\\hello.wav",future);
- exec.enqueue(play);
- int rc=-1;
- future->get(rc);
- delete future; future=NULL; // after retrieving, delete the object!
- cout << "Got result = "<<rc<<endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment