The TBB documentation gives this example of using lambda expressions with parallel_for, but doesn't provide an example of using lambda expressions with tbb::task::enqueue
.
I am looking for a simple example of tbb::task::enqueue
with a lambda expression.
Low-level tasks in TBB do not directly support lambda expressions. But with some extra coding you might create syntax-sugar helpers to do what you want.
You'd need to create a task class that calls a given functor:
And then, you'd need to create a function template that takes a functor/lambda, wraps it into
lambda_task
, and enqueues:And then you might use this function with lambda expressions:
The official TBB API classes that support lambda expressions, such as
task_group
andtask_arena
, use very similar code internally.Update: to pass a function pointer and arguments to call it with, the above approach can be extended in some ways:
tbb_enqueue_lambda
functionstd::tuple
insidelambda_task
, and 'unpacking' those for the function call. Unpacking is not trivial, but there are a few SO topics covering that already: "unpacking" a tuple to call a matching function pointer, How do I expand a tuple into variadic template function's arguments?, and other.