You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

multitaskingExample.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <cstdlib>
  6. #include "../CodeDweller/timing.hpp"
  7. #include "multitasking.hpp"
  8. using namespace std;
  9. class TaskHistory : public vector<string> {
  10. public:
  11. void post(string stuff) {
  12. push_back(stuff);
  13. }
  14. };
  15. string makeProgressMessage(int m) {
  16. stringstream messageBuilder;
  17. messageBuilder << "Making Progress (" << m << ")";
  18. return messageBuilder.str();
  19. }
  20. class TestTask : public MultiTask {
  21. private:
  22. string myName;
  23. TaskHistory& myHistory;
  24. Timeout myReadyTimeout;
  25. MultiTaskState task() {
  26. if(false == myReadyTimeout.isExpired()) return Waiting;
  27. int myChangedMind = rand() % 100;
  28. MultiTaskState myNewState = Running;
  29. switch(myChangedMind) {
  30. case 0: {
  31. myHistory.post("I'm Finished!");
  32. myNewState = Finished;
  33. break;
  34. }
  35. case 1:
  36. case 3:
  37. case 5:
  38. case 51:
  39. case 53:
  40. case 55:
  41. case 71:
  42. case 73:
  43. case 75:
  44. case 9: {
  45. myHistory.post("I need a nap...");
  46. myReadyTimeout.restart();
  47. myNewState = Waiting;
  48. break;
  49. }
  50. default: {
  51. myHistory.post(makeProgressMessage(myChangedMind));
  52. myNewState = Running;
  53. break;
  54. }
  55. }
  56. return myNewState;
  57. }
  58. static const int napDuration = 10;
  59. public:
  60. TestTask(string theName, TaskHistory& theHistory) :
  61. myName(theName),
  62. myHistory(theHistory),
  63. myReadyTimeout(napDuration) {
  64. }
  65. string name() { return myName; }
  66. };
  67. void outputHistory(TaskHistory& H) {
  68. for(vector<string>::iterator s = H.begin(); s != H.end(); ++s) {
  69. cout << *s << endl;
  70. }
  71. }
  72. int main() {
  73. unsigned long seed = 1234;
  74. srand(seed);
  75. cout << "MultiTasker Demo! seed(" << seed << ")" << endl;
  76. TaskHistory H0, H1, H2, H3, H4, H5, H6, H7, H8, H9;
  77. TestTask T0("Random Job 0", H0);
  78. TestTask T1("Random Job 1", H1);
  79. TestTask T2("Random Job 2", H2);
  80. TestTask T3("Random Job 3", H3);
  81. TestTask T4("Random Job 4", H4);
  82. TestTask T5("Random Job 5", H5);
  83. TestTask T6("Random Job 6", H6);
  84. TestTask T7("Random Job 7", H7);
  85. TestTask T8("Random Job 8", H8);
  86. TestTask T9("Random Job 9", H9);
  87. MultiTasker M0;
  88. M0.put(T0); cout << "Pending Count: " << M0.pending() << endl;
  89. M0.put(T1); cout << "Pending Count: " << M0.pending() << endl;
  90. M0.put(T2); cout << "Pending Count: " << M0.pending() << endl;
  91. M0.put(T3); cout << "Pending Count: " << M0.pending() << endl;
  92. M0.put(T4); cout << "Pending Count: " << M0.pending() << endl;
  93. M0.put(T5); cout << "Pending Count: " << M0.pending() << endl;
  94. M0.put(T6); cout << "Pending Count: " << M0.pending() << endl;
  95. M0.put(T7); cout << "Pending Count: " << M0.pending() << endl;
  96. M0.put(T8); cout << "Pending Count: " << M0.pending() << endl;
  97. M0.put(T9); cout << "Pending Count: " << M0.pending() << endl;
  98. PollTimer NothingToDo(10,50);
  99. while(M0.hasWork()) {
  100. M0.task();
  101. cout
  102. << "In Work: " << M0.pending()
  103. << ", Working: " << M0.working.size()
  104. << ", Waiting: " << M0.waiting.size()
  105. << ", Finished: " << M0.finished.size();
  106. if(M0.isReady()) {
  107. cout << endl;
  108. NothingToDo.reset();
  109. }
  110. else if(M0.isWaiting()) {
  111. cout << " pause..." << endl;
  112. NothingToDo.pause();
  113. }
  114. }
  115. cout << endl << endl << "M0 Complete Order..." << endl;
  116. bool firstT = true;
  117. while(M0.canGet()) {
  118. TestTask& T = (TestTask&) M0.get();
  119. if(firstT) { firstT = false; }
  120. else { cout << ", "; }
  121. cout << T.name();
  122. }
  123. cout << endl << endl << "T0(" << T0.name() << ") History..." << endl;
  124. outputHistory(H0);
  125. cout << endl << endl << "T1(" << T1.name() << ") History..." << endl;
  126. outputHistory(H1);
  127. cout << endl << endl << "T2(" << T2.name() << ") History..." << endl;
  128. outputHistory(H2);
  129. cout << endl << endl << "T3(" << T3.name() << ") History..." << endl;
  130. outputHistory(H3);
  131. cout << endl << endl << "T4(" << T4.name() << ") History..." << endl;
  132. outputHistory(H4);
  133. cout << endl << endl << "T5(" << T5.name() << ") History..." << endl;
  134. outputHistory(H5);
  135. cout << endl << endl << "T6(" << T6.name() << ") History..." << endl;
  136. outputHistory(H6);
  137. cout << endl << endl << "T7(" << T7.name() << ") History..." << endl;
  138. outputHistory(H7);
  139. cout << endl << endl << "T8(" << T8.name() << ") History..." << endl;
  140. outputHistory(H8);
  141. cout << endl << endl << "T9(" << T9.name() << ") History..." << endl;
  142. outputHistory(H9);
  143. return 0;
  144. }