Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node
- {
- int data; // or w/e
- Node * left;
- Node * right;
- template <typname Action>
- void SerialTraverse(Action action)
- {
- action(data); // Pre-order traversal
- if (left) left->SerialTraverse(action);
- // Traverse the left
- if (right) right->SerialTraverse(action);
- // Traverse the right
- }
- }
- namespace library
- {
- template<typename Task, typename Class, typename Arg>
- std::future<void> enqueue_task(Task task, Class * obj, Arg arg);
- // on some other thread, call "obj->task(arg);"
- // returns a handle upon which we can wait
- }
- template <typname Action>
- void ParallelTraverse(Action action)
- {
- action(data);
- std::future<void> fut;
- if (left) fut = library::enqueue_task(ParallelTraverse, left, action);
- // start traversing left on another thread
- if (right) right->ParallelTraverse(action);
- // traverse right on this thread, in parallel to traversing left
- if (fut.valid()) fut.wait();
- // wait for the left traversal to end
- }
Add Comment
Please, Sign In to add comment