I want to use
Ace
And
Loki
Library Design a similar
OpenMP
Two days ago, I thought about it carefully and outlined a general framework for discussion ;)
Before reading the following code, familiarize yourself
Loki
Curry faces
Functor
Use of the template class (《
C ++
The new design thinking book has a detailed description, but you should be prepared to read this book :). Otherwise, it will be confusing;
Ace
Is not described.
Paralleltask
Class, so
Ace
It doesn't matter if you don't know.
In combination with the following code, let's talk about the functions of each class:
(1) member functions in task1, task2, and task3
These three forms basically list several representation forms with function functions in C ++, and executing a function is to execute a task. The following describes the concept of serial and parallel tasks. In addition, the returned values are both
Void is used, and outgoing parameters are not passed in because these are abstract concepts and there is always a way to abstract tasks to these forms.
(2) about typedef
LOKI: functor <> serialtask, used here
LOKI: functor <>. I don't want to explain it in detail. Read the book. The approximate use is to use this template class. You can set the above three types (actually more) function Form is perfectly packaged for function execution
Of
Pre-
Slave tasks (prepare objects and input parameters, and so on) are separated from the execution process (that is, the execution function body), so that we can well separate the execution period and execution period of the task, convenient macro design. If
Understanding the command mode in the design mode should be quickly understood. In addition, using a chain template function, this template class can also easily serialize each task to implement the serial function.
Set its typedef
Serialtask.
(3) about classes
Paralleltask
As the name suggests, this class has a parallel function class. I can use its pushtask member function to easily plug tasks into it. These tasks will be executed in parallel. When to execute them, you can call
Operator. This class can also be viewed as a task (composed of many small tasks), and with operator (), it can also be conveniently handed over to the serialtask object to serial it with other tasks. The implementation behind this class will use ACE, because Ace itself has a powerful cross-platform multi-threaded library.
(4)
Sample1_2_3 (),
Sample000023 ()
, Sample000023 (),
Sample123 () is a few examples I have written. With these examples, I can better understand my design intent.
1. void task1 (); <br/> 2. struct task2 <br/> 3. {<br/> 4. void operator (); <br/> 5 .}; <br/> 6. struct task3 <br/> 7. {<br/> 8. void memberfun (); <br/> 9 .}; <br/> 10. typedef LOKI: functor <> serialtask; <br/> 11. <br/> 12. class paralleltask <br/> 13. {<br/> 14. public: <br/> 15. paralleltask (bool bwait = true); <br/> 16. void operator (); <br/> 17. void pushtask (const serialtask & St); <br/> 18. PRIVATE: <br/> 19. vector <serialtask> m_serialtaskvec; <br/> 20. bool m_bwait; <br/> 21 .}; <br/> 22. <br/> 23. serialtask subtask1 (task1); // The subtask1 object runs the task1 () function. <Br/> 24. <br/> 25. task2 T2; <br/> 26. serialtask subtask2 (T2); // The subtask2 object will run T2 (), that is, run t2: Operator (). <Br/> 27. <br/> 28. task3 T3; <br/> 29. serialtask subtask3 (& T3, & task3: memberfun); // The subtask3 object will be t3.memberfun (). <Br/> 30. <br/> 31. serialtask subtask1_2 (chain (subtask1, subtask2); // The subtask1_2 object will be executed serially in subtask1 and subtask2, that is, Task 1 () and T2: Operator (). <Br/> 32. <br/> 33. serialtask subtask1_2_3 (chain (subtask1_2, subtask3); // The subtask1_2_3 object will be executed serially in subtask1, subtask2, and subtask3, that is, Task 1 () and T2 :: operator (), and finally execute t3.memberfun (). <Br/> 34. <br/> 35. // call subtask1_2_3 () to execute tasks 1, 2, 3 in sequence. <br/> 36. void sample1_2_3 () <br/> 37. {<br/> 38. subtask1_2_3 (); <br/> 39 .} <br/> 40. <br/> 41. // This function indicates that task 1 is executed first, and then tasks 2 and 3 are executed in parallel <br/> 42. void sample000023 () <br/> 43. {<br/> 44. paralleltask pt; <br/> 45. PT. pushtask (subtask2); <br/> 46. PT. pushtask (subtask3); <br/> 47. <br/> 48. serialtask ST (chain (subtask1, pt); // note that PT is added as a serial task. <br/> 49. <br/> 50. ST (); <br/> 51 .} <br/> 52. <br/> 53. // This function indicates parallel execution of tasks 1, 2, and 3 <br/> 54. void sample123 () <br/> 55. {<br/> 56. paralleltask pt; <br/> 57. PT. pushtask (subtask1); <br/> 58. PT. pushtask (subtask2); <br/> 59. PT. pushtask (subtask3); <br/> 60. <br/> 61. pt (); <br/> 62 .}