Guest User

Untitled

a guest
Apr 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. class Node
  2. {
  3. int data; // or w/e
  4.  
  5. Node * left;
  6. Node * right;
  7.  
  8. template <typname Action>
  9. void SerialTraverse(Action action)
  10. {
  11. action(data); // Pre-order traversal
  12.  
  13. if (left) left->SerialTraverse(action);
  14. // Traverse the left
  15. if (right) right->SerialTraverse(action);
  16. // Traverse the right
  17. }
  18. }
  19.  
  20. namespace library
  21. {
  22. template<typename Task, typename Class, typename Arg>
  23. std::future<void> enqueue_task(Task task, Class * obj, Arg arg);
  24. // on some other thread, call "obj->task(arg);"
  25. // returns a handle upon which we can wait
  26. }
  27.  
  28. template <typname Action>
  29. void ParallelTraverse(Action action)
  30. {
  31. action(data);
  32. std::future<void> fut;
  33. if (left) fut = library::enqueue_task(ParallelTraverse, left, action);
  34. // start traversing left on another thread
  35.  
  36. if (right) right->ParallelTraverse(action);
  37. // traverse right on this thread, in parallel to traversing left
  38.  
  39. if (fut.valid()) fut.wait();
  40. // wait for the left traversal to end
  41. }
Add Comment
Please, Sign In to add comment