Using pthread in Android

来源:互联网 发布:变性人有快感吗 知乎 编辑:程序博客网 时间:2024/05/29 04:39

【原创文章,转载请保留或注明出处: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)

原创粉丝点击