黑龙江建设兵团知青网站,网站底部的备案信息修改,山东网站营销seo哪家好,大连网站建设哪个公司好文章目录 Android native层的线程分析(C)#xff0c;多线程实现1.native线程的创建第一部分#xff1a;android_thread模块第二部分#xff1a;linux_thread模块 2.测试linux_thread模块3.Android native的Thread类3.1源码分析 4.native层堆栈调试方法1. 引用库2. 头文件3. … 文章目录 Android native层的线程分析(C)多线程实现1.native线程的创建第一部分android_thread模块第二部分linux_thread模块 2.测试linux_thread模块3.Android native的Thread类3.1源码分析 4.native层堆栈调试方法1. 引用库2. 头文件3. 调用代码 Android native层的线程分析(C)多线程实现
1.native线程的创建
pthread_t //表示线程ID
pthread_equal (pthread_t __thread1, pthread_t __thread2);//比较线程ID
pthread_t pthread_self (void);//用户返回线程ID
pthread_create()//线程创建
编写Android.mk文件
这个MK文件是一个Android.mk构建脚本用于指导Android Native Development Kit (NDK)如何编译和链接两个可执行模块android_thread 和 linux_thread。下面是该脚本的详细解析
LOCAL_PATH : $(call my-dir)
include $(CLEAR_VARS)LOCAL_SRC_FILES : MyThread.cpp \Main.cpp \LOCAL_SHARED_LIBRARIES :libandroid_runtime \libcutils \libutils \liblog LOCAL_MODULE : android_threadLOCAL_PRELINK_MODULE : falseinclude $(BUILD_EXECUTABLE)include $(CLEAR_VARS)LOCAL_SRC_FILES : thread_posix.c LOCAL_MODULE : linux_thread
LOCAL_SHARED_LIBRARIES :liblog LOCAL_PRELINK_MODULE : falseinclude $(BUILD_EXECUTABLE)//把thread_posix.c 文件编译(BUILD_EXECUTABLE)成为一个二进制的可执行文件这个二进制可执行文件的名字是linux_thread。第一部分android_thread模块
LOCAL_PATH: 这一行定义了当前目录的路径通过my-dir函数自动获取作为查找其他文件如源代码文件的相对路径基础。include $(CLEAR_VARS): 这行代码包含了清除所有之前定义的LOCAL变量的脚本确保为新模块提供一个干净的构建环境。LOCAL_SRC_FILES: 指定了要编译的源文件列表。在这个例子中包括MyThread.cpp 和 Main.cpp。LOCAL_SHARED_LIBRARIES: 列出了该模块需要链接的共享库包括libandroid_runtime, libcutils, libutils, 和 liblog。这些都是Android系统提供的库用于支持Android运行时功能、实用工具函数、日志输出等功能。LOCAL_MODULE: 定义了模块的名称这里是android_thread。LOCAL_PRELINK_MODULE: 设定为false表示该模块在预链接阶段不会被处理。预链接是一个可选步骤通常用于减少应用启动时间但在这里不适用。include $(BUILD_EXECUTABLE): 告诉NDK构建系统根据前面定义的规则将这些源文件编译成一个可执行文件。
第二部分linux_thread模块
这部分结构与第一部分相似但是针对另一个模块linux_thread
LOCAL_SRC_FILES 只包含一个源文件thread_posix.c意味着这是一个基于POSIX线程标准实现的模块。LOCAL_SHARED_LIBRARIES 只列出了liblog说明这个模块依赖于日志库来记录日志信息。其他如LOCAL_MODULE、LOCAL_PRELINK_MODULE以及最后的include $(BUILD_EXECUTABLE)指令用法与android_thread模块相同用于构建名为linux_thread的独立可执行模块。
综上所述这个MK文件配置了两个C/C语言编写的可执行模块的构建过程一个是与Android系统紧密结合的android_thread另一个是使用POSIX线程API的linux_thread两者都将作为独立的可执行文件生成。
thread_posix.c
这段代码是一个简单的C语言程序演示了如何在Linux或类Unix系统中使用POSIX线程库pthread.h创建并管理一个线程。同时它也使用了Android的日志系统通过utils/Log.h头文件来记录日志信息。以下是代码的详细解释
#include pthread.h // 包含POSIX线程库头文件
#include stdlib.h // 用于exit函数
#include stdio.h // 用于printf函数
#include utils/Log.h // 包含Android日志系统头文件// 定义线程执行的函数
void *thread_posix_function(void *arg) {(void*)arg; // 忽略传入的参数这里没有使用int i;for (i 0; i 30; i) {printf(hello thread i %d\n, i); // 打印到标准输出ALOGD(hello thread i %d\n, i); // 使用Android日志系统打印DEBUG级别日志sleep(1); // 线程暂停1秒}return NULL; // 线程函数结束返回空指针
}int main(void) {pthread_t mythread; // 定义一个线程标识符// 创建一个新的线程执行thread_posix_function函数传入参数为NULLif (pthread_create(mythread, NULL, thread_posix_function, NULL)) {ALOGD(error creating thread.); // 如果创建失败记录错误日志abort(); // 终止程序执行}// 主线程等待mythread线程结束if (pthread_join(mythread, NULL)) {ALOGD(error joining thread.); // 如果等待失败记录错误日志abort(); // 终止程序执行}ALOGD(hello thread has run end exit\n); // 记录日志表明线程已正确执行完毕exit(0); // 主程序正常退出
}
这段代码首先定义了一个线程执行的函数thread_posix_function该函数每隔一秒打印一次消息到控制台和Android日志系统共打印30次。在main函数中它创建了一个新的线程并执行thread_posix_function然后主线程等待这个新线程完成其任务后才退出。整个过程中还利用了Android的日志系统来报告错误或提供执行状态信息。
2.测试linux_thread模块 在安卓源码的根目录下创建一个文件夹并写上mk文件提供编译的脚本。
然后单独编译模块名即可 LOCAL_MODULE 把编译后的可执行二进制文件push到设备中 执行一下可以看到线程在打印输出。 3.Android native的Thread类
Android native的Thread类是Android提供的一个基础类
system\core\libutils\include\utils\Thread.h system\core\libutils\Threads.cpp
智能指针主要用来释放和控制内存。 virtual void onFirstRef();第一次这个类被创建就会执行这个智能指针的这个方法在这个方法里面我们就可以做一些事情了。执行线程创建并启动运行un方法status_t run(const char* name, int32_t priority, size_t stack);先执行readyToRun()创建完成后通过调用threadLoop()函数线程请求退出方法实现requestExit()函数。
3.1源码分析
run方法可以看到它也是用的pthread那套线程apiAndroid的native层的线程就是基于linux的pthread方案进行封装的。 进入_threadLoop
int Thread::_threadLoop(void* user)
{Thread* const self static_castThread*(user);spThread strong(self-mHoldSelf);wpThread weak(strong);self-mHoldSelf.clear();#if defined(__ANDROID__)// this is very useful for debugging with gdbself-mTid gettid();
#endifbool first true;do {bool result;if (first) {first false;//我们一旦调用了线程的run方法之后首先执行的就是这个readyToRun方法。self-mStatus self-readyToRun();result (self-mStatus OK);if (result !self-exitPending()) {// Binder threads (and maybe others) rely on threadLoop// running at least once after a successful ::readyToRun()// (unless, of course, the thread has already been asked to exit// at that point).// This is because threads are essentially used like this:// (new ThreadSubclass())-run();// The caller therefore does not retain a strong reference to// the thread and the thread would simply disappear after the// successful ::readyToRun() call instead of entering the// threadLoop at least once.result self-threadLoop();}} else {result self-threadLoop();}// establish a scope for mLock{Mutex::Autolock _l(self-mLock);if (result false || self-mExitPending) {self-mExitPending true;self-mRunning false;// clear thread ID so that requestExitAndWait() does not exit if// called by a new thread using the same thread ID as this one.self-mThread thread_id_t(-1);// note that interested observers blocked in requestExitAndWait are// awoken by broadcast, but blocked on mLock until break exits scopeself-mThreadExitedCondition.broadcast();break;}}// Release our strong reference, to let a chance to the thread// to die a peaceful death.strong.clear();// And immediately, re-acquire a strong reference for the next loopstrong weak.promote();} while(strong ! nullptr);return 0;
}测试
#ifndef _MYTHREAD_H
#define _MYTHREAD_H#include utils/threads.hnamespace android {class MyThread: public Thread {
public:MyThread();virtual void onFirstRef();virtual status_t readyToRun();virtual bool threadLoop();virtual void requestExit();
private:int hasRunCount 0;
};}
#endif#define LOG_TAG MyThread#include utils/Log.h
#include MyThread.hnamespace android {MyThread::MyThread() :Thread(false) {ALOGD(MyThread);}bool MyThread::threadLoop() {ALOGD(threadLoop hasRunCount %d,hasRunCount);hasRunCount;if (hasRunCount 10) {return false; }return true;}void MyThread::onFirstRef() {ALOGD(onFirstRef);}status_t MyThread::readyToRun() {ALOGD(readyToRun);return 0;}void MyThread::requestExit() {ALOGD(requestExit);}
}同上linux_thread操作编译push即可。
4.native层堆栈调试方法
android::CallStack()。所在线程的堆栈调用打印出来。
进入对应的cpp文件解开注释并且修改值为1 声明头文件 调用方法 android::CallStack cs(zxx);cs.update();cs.log(zxx,ANDROID_LOG_ERROR,);测试 1. 引用库
cc_defaults {name: bootanimation_defaults,...shared_libs: [libandroidfw,libbase,libbinder,libcutils,liblog,libutils,# 库libutilscallstack],
}
2. 头文件
#include utils/CallStack.h
3. 调用代码 android::CallStack cs(zxx);cs.update();cs.log(zxx,ANDROID_LOG_ERROR,);