Guest User

Untitled

a guest
Apr 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1.  
  2. /*
  3. This code shows how to perform a async/wait-for-result method, with ACE Activation Queues.
  4. Works on windows.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <ace/Task.h>
  9. #include <ace/Activation_Queue.h>
  10. #include <ace/Method_Request.h>
  11. #include <ace/Future.h>
  12. #include <iostream>
  13. #include <string>
  14.  
  15. using namespace std;
  16.  
  17. class Executor;
  18.  
  19. typedef ACE_Future<int> future_result;
  20.  
  21. struct MethodProxy : public ACE_Method_Request
  22. {
  23. //friend Executor;
  24. int call() { return 0; }
  25. future_result * future_;
  26. public:
  27. MethodProxy() : future_(NULL) {}
  28. virtual int call(Executor*)=0;
  29. };
  30.  
  31. struct Play : public MethodProxy
  32. {
  33. string filename_;
  34. Play(string filename, future_result * f=NULL) : filename_(filename) {
  35. future_=f;
  36. }
  37. int call(Executor * thiz_) {
  38. cout << "Playing " << filename_ << endl;
  39. Sleep(1000);
  40. return 666;
  41. }
  42. };
  43.  
  44.  
  45. class Executor : public ACE_Task_Base
  46. {
  47. ACE_Activation_Queue queue_;
  48.  
  49. int svc()
  50. {
  51. int rc=0;
  52. ACE_Method_Request * method=NULL;
  53. MethodProxy * mp=NULL;
  54. while(true)
  55. {
  56. mp=(MethodProxy*)queue_.dequeue();
  57. if(NULL==mp) continue;
  58. rc=mp->call(this);
  59. if(mp->future_)
  60. mp->future_->set(rc);
  61. delete mp; // we are responsible for MethodProxy objects, but not for futures!
  62. }
  63. }
  64.  
  65. public:
  66. int enqueue(MethodProxy * method)
  67. {
  68. return queue_.enqueue(method);
  69. }
  70. };
  71.  
  72.  
  73. int main(int argc, char* argv[])
  74. {
  75. Executor exec;
  76. exec.activate();
  77.  
  78. future_result * future = new future_result;
  79. Play *play = new Play("c:\\hello.wav",future);
  80. exec.enqueue(play);
  81. int rc=-1;
  82. future->get(rc);
  83. delete future; future=NULL; // after retrieving, delete the object!
  84. cout << "Got result = "<<rc<<endl;
  85. return 0;
  86. }
Add Comment
Please, Sign In to add comment