Using pthread in Android

本文详细介绍了在Android系统中通过Linux C编程和NDK工具实现无阻塞socket操作的方法,利用pthread进行多线程编程,并通过Android.mk文件构建可执行应用。同时,解决在Android设备上运行自定义程序时遇到的权限问题,最终成功运行应用并展示了pthread在Android平台的可用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【原创文章,转载请保留或注明出处:http://blog.sina.com.cn/s/blog_675eba3901010tnk.html

I have a project about some system-layer in Android mainly using linux C programming and NDK tools. For using un-blocking socket  ,I must use multi-threads but under Dalvik layer I can't use Java thread mechanism . So that I have to use pthread and using ndk cross-complie toolchains to complie the src for my android phone. Fortunalety,I succeed and at least it is proved that it's avaiable for android to use pthread.

 

The source code is too simple to pasted on ....

 

//src hello-jni.c ------------------------------------

#include <string.h>
#include <jni.h>
#include <pthread.h>

void threadmethod()
{
   printf("wtf...");
}
int main(void)
{
 //insloop();
 int ret;
 pthread_t id;
 ret=pthread_create(&id,NULL,(void*)threadmethod,NULL);
 pthread_join(id,NULL);
 printf("I am the main thread!!!");
}

 

And the Android.mk 's content is listed as below:

LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.c
LOCAL_LDLIBS += -llog -lpthread

include $(BUILD_EXECUTABLE)

 

Notice the last line , I want to build a executable app not a lib .

Don't forget add the pthread lib .

Then use ndk's command or script "ndk-build " to build an app ,push it to your phone.

Firstly I push the file in my sdcard ,but in adb-shell when I exec the command "chmod 777 hello-jni" it always fails .

Then I push into folder /data/data and exec the same command successfully .

I don't know why...

Then you can run your app on root authenticate.

With the Screenshot attached :

 

Using <wbr>pthread <wbr>in <wbr>Android

ps : my OS is ubuntu 10.10 with ndk-r7

       my phone is htc desire Z with android 2.3.3(the kernel is 2.6.35)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值