Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2477 Discussions

How do I temporarily allow main thread to participate in flow graph?

JonCz
Beginner
532 Views

I have a tbb application in that looks like this:

 

int main() {
graph g;
input_node<Msg> input(g);
function_node<Msg,int> long_work_node( g, unlimited, [] {
... do work, task cannot be broken up ...
graph_result.push(result_value);
return 0;
});

make_edge( input, long_work_node );
input.activate()

// try getting output from graph_output
int result;
while( ... running ... ) {
bool got_result = graph_result.try_pop(result);
if(got_result) {
... use results ...
} else {
... wait and do nothing ...
}
}
g.wait_for_all();
return 0;
}

A lot of the time the main thread is spinning and doing nothing. I would like the main thread to "moonlight" and participate in the graph, but only temporarily (i.e., I don't want to wait until the graph to finish completely).

Are there simple code patterns to do what I'm trying to? How do I achieve this?

Any help appreciated!

0 Kudos
3 Replies
Mark_L_Intel
Moderator
395 Views

Hello @JonCz ,

 

 Flow graph should use the master thread for node computations. How have you determined that the main thread is just spinning? Have you used Vtune? You could also create a task_group inside the node.   

0 Kudos
Mark_L_Intel
Moderator
340 Views

Hello @JonCz ,

 

  As I said above, the main thread should work in general (for the flow graph). However, if you're trying to add your own internal queue and wait on it (at least it looks like your intent) --   

bool got_result = graph_result.try_pop(result);
 if(got_result) {
... use results ...
} else {
... wait and do nothing ...
}

 please provide more details regarding this. 

0 Kudos
joanlee9494
Beginner
163 Views

To temporarily allow the main thread to participate in a flow graph:

1. **Suspend** the flow graph execution.
2. Execute main thread tasks.
3. **Resume** flow graph execution afterward.

0 Kudos
Reply