Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In order execution with TBB?

I would like to have something like a tbb::task_group but with the difference of guaranteed in order execution, e.g.

serial_task_group tasks;

tasks.run([]{std::cout << 1;});
tasks.run([]{std::cout << 2;});
tasks.run([]{std::cout << 3;});
tasks.wait();

// guaranteed output: 123

Any suggestions as to how one could achieve this using tbb?

Currently I have an explicit thread that just executes from a queue using a condition variable. However, the problem with using a queue is how I would go about guaranteeing that only exactly one task is active in the task_group.

like image 939
ronag Avatar asked Mar 27 '26 05:03

ronag


1 Answers

(Disclosure: I work on Intel Threading Building Blocks at Intel.)

As was suggested in another answer, you can use the flow graph to do something like this. The code for your example using the flow graph would look something like:

#include "tbb/flow_graph.h"
#include <iostream>

using namespace tbb::flow;

int main( int argc, char *argv[] ) {
    graph g;
    continue_node< continue_msg > n1( g, []( const continue_msg & ) { std::cout << "1"; } );
    continue_node< continue_msg > n2( g, []( const continue_msg & ) { std::cout << "2"; } );
    continue_node< continue_msg > n3( g, []( const continue_msg & ) { std::cout << "3"; } );
    make_edge( n1, n2 );
    make_edge( n2, n3 );
    n1.try_put( continue_msg() );
    g.wait_for_all();
   return 0;
}
like image 51
Mike Voss Avatar answered Mar 29 '26 21:03

Mike Voss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!