Android 教程:使用英特尔 线程构建模块编写多线程应用

moon8150 2014-05-22 04:51:30
近来,我们发布了 “Windows* 8 教程:使用英特尔 线程构建模块为 Windows Store* 编写多线程应用”。其中我们提到,并行计算引擎很容易导入其他移动或台式机平台。Android 是这种移动平台的一个典型的例子。

在最新发布的稳定版本的英特尔线程构建模块(英特尔TBB)中,我们尝试支持 Android;即构建英特尔 TBB 库,通过 JNI 界面在 Android 应用中使用。您可以从 threadingbuildingblocks.org 下载该版本。

如要在 Linux* 主机上启动流程,请打开英特尔 TBB 源发布,找到 <unpacked_dir>/build/android_setup.csh 脚本,并构建库。您必须构建库,因为开发版本仅可在源形式下发布。<unpacked_dir>/build/index.android.html文件包括在 Linux 上配置环境和构建库的说明。

假定 %PATH%(在 Microsoft Windows* 主机平台上)和 $PATH(在 Linux 平台上)中 gnu make 3.81 可用,我们需要在 NDK 环境中发布下列命令来为 Android 构建英特尔 TBB 库:

gmake tbb tbbmalloc target=android


构建库所需的操作只有这些;我们现在可以使用 Eclipse* 构建示例。对于下列的示例,我将使用 Windows* 上的 Android SDK 工具 Rev.21 和 Android NDK Rev 8C 来说明跨平台开发的流程。

使用默认模板《全新 Android 应用》创建新对象。为简单起见,我们将其命名为 “app1”,与上述的发布相同:

选择“FullscreenActivity”为“Activity”。有关该模板的内容到此为止。请注意不可以使用 com.example* 为 Google Play* 命名,但是它可以很好地帮助我们理解。

然后向主帧中添加几个按钮。添加后,主帧的 XML 文件(app1/res/layout/activity_fullscreen.xml)如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#0099cc"

tools:context=".FullscreenActivity" >

<TextView

android:id="@+id/fullscreen_content"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:keepScreenOn="true"

android:text="@string/dummy_content"

android:textColor="#33b5e5"

android:textSize="50sp"

android:textStyle="bold" />

<FrameLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true" >

<LinearLayout

android:id="@+id/fullscreen_content_controls"

style="?buttonBarStyle"

android:layout_width="match_parent"

android:layout_height="74dp"

android:layout_gravity="bottom|center_horizontal"

android:background="@color/black_overlay"

android:orientation="horizontal"

tools:ignore="UselessParent" >

<Button

android:id="@+id/dummy_button1"

style="?buttonBarButtonStyle"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/dummy_button1"

android:onClick="onClickSR" />

<Button

android:id="@+id/dummy_button2"

style="?buttonBarButtonStyle"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/dummy_button2"

android:onClick="onClickDR" />

</LinearLayout>

</FrameLayout>

</FrameLayout>

字符串文件(app1/res/values/strings.xml)如下
<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="app_name">Sample</string>

<string name="dummy_content">Reduce sample</string>

<string name="dummy_button1">Simple Reduce</string>

<string name="dummy_button2">Deterministic Reduce</string>

</resources>

然后添加按钮处理器:
// JNI functions

private native float onClickDRCall();

private native float onClickSRCall();



public void onClickDR(View myView) {

TextView tv=(TextView)(this.findViewById(R.id.fullscreen_content));

float res=onClickDRCall();

tv.setText("Result DR is n" + res);

}



public void onClickSR(View myView) {

TextView tv=(TextView)(this.findViewById(R.id.fullscreen_content));

float res=onClickSRCall();

tv.setText("Result SR is n" + res);

}

然后,将库加载至 FullscreenActivity.java 文件:
       @Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);



System.loadLibrary("gnustl_shared");

System.loadLibrary("tbb");

System.loadLibrary("jni-engine");

}

在使用 "tbb" 库时,应清除所有内容,且需要使用 "gnustl_shared" 库来支持 TBB 的 C++ 语言特性。但是,对于 "jni-engine" 库,我们需要获得更多内容。

"jni-engine" 是 С++ 库,可实施计算引擎名为 onClickSRCall() 和 onClickSRCall() 的 JNI 调用输出 C 接口。

根据 NDK 开发规则,在工作空间内创建文件夹 “jni”,并在该文件夹内专门针对 "jni-engine" 库创建 3 个文件。

请点击这个链接阅读详细介绍:http://g.csdn.net/5269626
...全文
164 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
哥特帝景 2014-05-26
  • 打赏
  • 举报
回复

567

社区成员

发帖
与我相关
我的任务
社区描述
英特尔® 边缘计算,聚焦于边缘计算、AI、IoT等领域,为开发者提供丰富的开发资源、创新技术、解决方案与行业活动。
社区管理员
  • 英特尔技术社区
  • shere_lin
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧