lambd 创建线程,线程构建块(TBB)使用lambda入队队列任务

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:

template

class lambda_task : public tbb::task {

F my_func;

/*override*/ tbb::task* execute() {

my_func();

return NULL;

}

public:

lambda_task( const F& f ) : my_func(f) {}

};

And then, you'd need to create a function template that takes a functor/lambda, wraps it into lambda_task, and enqueues:

template

void tbb_enqueue_lambda( const F& f ) {

tbb::task::enqueue( *new( tbb::task::allocate_root() ) lambda_task(f) );

}

And then you might use this function with lambda expressions:

tbb_enqueue_lambda( []{ /* code here */ } );

The official TBB API classes that support lambda expressions, such as task_group and task_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:

In C++03, you'd need to add separate class templates for a task with one argument, two arguments, etc., and corresponding overloads of tbb_enqueue_lambda function

In C++11, you could use variadic templates, storing the actual arguments in an std::tuple inside lambda_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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Lambda表达式可以简化Java代码,并且可以使代码更加易读和易于维护。下面以Java 8为例,演示如何使用Lambda表达式查询菜单树表: ```java import java.sql.*; import java.util.ArrayList; import java.util.List; public class MenuTree { public static void main(String[] args) { try { // 连接数据库 Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); // 执行查询语句,并使用Lambda表达式对ResultSet进行处理 List<Menu> menus = new ArrayList<>(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT id, name, ParentId, delectableflag FROM menu"); while (rs.next()) { Menu menu = new Menu( rs.getInt("id"), rs.getString("name"), rs.getInt("ParentId"), rs.getBoolean("delectableflag") ); menus.add(menu); } // 关闭连接 rs.close(); stmt.close(); conn.close(); // 根据菜单项构建菜单树,使用Lambda表达式进行递归 List<Menu> menuTree = buildMenuTree(menus, 0); menuTree.forEach(Menu::print); } catch (Exception e) { e.printStackTrace(); } } private static List<Menu> buildMenuTree(List<Menu> menus, int parentId) { List<Menu> result = new ArrayList<>(); menus.stream() .filter(menu -> menu.getParentId() == parentId) .forEach(menu -> { List<Menu> children = buildMenuTree(menus, menu.getId()); menu.setChildren(children); result.add(menu); }); return result; } } class Menu { private int id; private String name; private int parentId; private boolean delectableflag; private List<Menu> children; public Menu(int id, String name, int parentId, boolean delectableflag) { this.id = id; this.name = name; this.parentId = parentId; this.delectableflag = delectableflag; } public int getId() { return id; } public String getName() { return name; } public int getParentId() { return parentId; } public boolean isDelectable() { return delectableflag; } public List<Menu> getChildren() { return children; } public void setChildren(List<Menu> children) { this.children = children; } public void print() { print(0); } private void print(int level) { System.out.println(String.format("%s%s (%d)", " ".repeat(level * 2), name, id)); if (children != null) { for (Menu child : children) { child.print(level + 1); } } } } ``` 以上代码中,使用Lambda表达式对ResultSet进行处理,将每个菜单项构建成Menu对象,并将它们添加到列表中。然后,使用Lambda表达式对列表进行递归,根据每个菜单项的parentId构建菜单树。最后,使用Lambda表达式输出菜单树。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值