21 #ifndef __TBB_parallel_while 22 #define __TBB_parallel_while 29 template<
typename Body>
35 template<
typename Stream,
typename Body>
class while_task;
40 template<
typename Body>
41 class while_iteration_task:
public task {
43 typename Body::argument_type my_value;
48 while_iteration_task(
const typename Body::argument_type& value,
const Body& body ) :
49 my_body(body), my_value(value)
51 template<
typename Body_>
friend class while_group_task;
58 template<
typename Body>
59 class while_group_task:
public task {
60 static const size_t max_arg_size = 4;
63 typename Body::argument_type my_arg[max_arg_size];
64 while_group_task(
const Body& body ) : my_body(body), size(0) {}
66 typedef while_iteration_task<Body> iteration_type;
67 __TBB_ASSERT( size>0, NULL );
72 t =
new( allocate_child() ) iteration_type(my_arg[k],my_body);
73 if( ++k==size )
break;
76 set_ref_count(
int(k+1));
78 spawn_and_wait_for_all(*t);
81 template<
typename Stream,
typename Body_>
friend class while_task;
87 template<
typename Stream,
typename Body>
88 class while_task:
public task {
91 empty_task& my_barrier;
93 typedef while_group_task<Body> block_type;
94 block_type& t = *
new( allocate_additional_child_of(my_barrier) ) block_type(my_body);
96 while( my_stream.pop_if_present(t.my_arg[k]) ) {
97 if( ++k==block_type::max_arg_size ) {
99 recycle_to_reexecute();
111 while_task( Stream& stream,
const Body& body, empty_task& barrier ) :
128 template<
typename Body>
137 my_barrier->destroy(*my_barrier);
149 template<
typename Stream>
150 void run( Stream& stream,
const Body& body );
154 void add(
const value_type& item );
158 empty_task* my_barrier;
161 template<
typename Body>
162 template<
typename Stream>
165 empty_task& barrier = *
new( task::allocate_root() ) empty_task();
167 my_barrier = &barrier;
168 my_barrier->set_ref_count(2);
169 while_task<Stream,Body>& w = *
new( my_barrier->allocate_child() ) while_task<Stream,Body>( stream, body, barrier );
170 my_barrier->spawn_and_wait_for_all(w);
171 my_barrier->destroy(*my_barrier);
176 template<
typename Body>
178 __TBB_ASSERT(my_barrier,
"attempt to add to parallel_while that is not running");
179 typedef internal::while_iteration_task<Body> iteration_type;
180 iteration_type& i = *
new( task::allocate_additional_child_of(*my_barrier) ) iteration_type(item,*my_body);
181 task::self().spawn( i );
~parallel_while()
Destructor cleans up data members before returning.
Definition: parallel_while.h:135
void add(const value_type &item)
Add a work item while running.
Definition: parallel_while.h:177
Parallel iteration over a stream, with optional addition of more work.
Definition: parallel_while.h:30
Body::argument_type value_type
Type of items.
Definition: parallel_while.h:143
parallel_while()
Construct empty non-running parallel while.
Definition: parallel_while.h:132
void run(Stream &stream, const Body &body)
Apply body.apply to each item in the stream.
Definition: parallel_while.h:163
Definition: _flow_graph_async_msg_impl.h:32
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44