当前位置: 首页 > ops >正文

android关于pthread的使用过程

文章目录

  • 简介
  • 代码流程
    • pthread使用
    • hello_test.cpp
    • Android.bp
  • 编译过程报错处理
  • 验证过程

简介

android开发经常需要使用pthread来编写代码实现相关的业务需求

代码流程

pthread使用

需要查询某个linux函数的方法使用,可以使用man + 函数名

// $ man pthread_create#include <pthread.h>int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
// $ man pthread_detach#include <pthread.h>int pthread_detach(pthread_t thread);

hello_test.cpp

#include <utils/Log.h>
#include <pthread.h>
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "hello_test"pthread_t thread_id;void deal_data(){ALOGD("deal_data");
}static void *start_thread_func (void *arg){ALOGD("start_thread_func");pthread_detach(thread_id);deal_data();return nullptr;
}int main(int args,char** argv) {ALOGD("main");if(pthread_create(&thread_id,nullptr,start_thread_func,nullptr/**this**/) == 0){ALOGD("pthread_create success");}else{ALOGD("pthread_create failed");}sleep(5);ALOGD("main end");return 0;
}

Android.bp

cc_binary{name:"hello_test",srcs:["hello_test.cpp",],shared_libs:["liblog","libutils",],cflags: ["-Wno-error","-Wno-unused-parameter",],
}

编译过程报错处理

cflags的配置
在这里插入图片描述
从编译报错可以看出是unused parameter ‘xxx’ [-Werror,-Wunused-parameter],刚开始以为直接配置报错的参数就好了,发现没有用,下次编译还是报错。

百度一波,说明如下:
解决-Werror,-Wunused类似问题万能公式 将"-Wunused-parameter"形式修改为"-Wno-unused-parameter"形式。其关键是将"-W"修改为:-Wno-,后边保持不变即可。
即可编译成功。

验证过程

adb push hello_test /system/bin
执行

/system/bin # ./hello_test

可以看到日志可以正常输出,打印如下所示

06-08 15:40:20.727  1978  1978 D hello_test: main
06-08 15:40:20.728  1978  1978 D hello_test: pthread_create success
06-08 15:40:20.728  1978  1979 D hello_test: start_thread_func
06-08 15:40:20.728  1978  1979 D hello_test: deal_data
06-08 15:40:25.728  1978  1978 D hello_test: main end
http://www.xdnf.cn/news/12732.html

相关文章:

  • 【CBAP50技术手册】#39 Roles and Permissions Matrix(角色与权限矩阵):业务分析师的“秩序守护器”
  • 横向对比npm和yarn
  • 基于Vue3.0的在线工具网站
  • 26考研——数据的表示和运算_整数和实数的表示(2)
  • (三)Linux性能优化-CPU-CPU 使用率
  • 强化学习选择rule-based的reward func还是使用reward model / RLAIF?
  • mq安装新版-3.13.7的安装
  • [2025CVPR]确定性图像转换新突破:双逼近器布朗桥模型(Dual-approx Bridge)技术详解
  • LangGraph--Agent工作流
  • 【iOS】 Block再学习
  • iOS 抖音导航栏首页一键分两列功能的实现
  • 2025-06-01-Hive 技术及应用介绍
  • CSS悬停闪现与a标签嵌套的问题
  • SQL手工测试(MySQL数据库)
  • 云原生技术驱动 IT 架构现代化转型:企业实践与落地策略全解
  • 网约车平台(预约打车)
  • 手动给中文分词和 直接用神经网络RNN做有什么区别
  • 使用 IntelliJ IDEA 安装通义灵码(TONGYI Lingma)插件,进行后端 Java Spring Boot 项目的用户用例生成及常见问题处理
  • OPENCV形态学基础之一膨胀
  • 数据结构---红黑树
  • 【大模型LLM学习】function call/agent学习记录
  • Windows开机自动启动中间件
  • CAD多面体密堆积3D插件
  • Maven的使用
  • Mac M芯片 RAG 极简流程 安装 ragflow + LM studio
  • Java 高级泛型实战:8 个场景化编程技巧
  • 0x-4-Oracle 23 ai-sqlcl 25.1.1 独立安装-配置和优化
  • OD 算法题 B卷【正整数到Excel编号之间的转换】
  • Web后端开发(请求、响应)
  • SpringCloud2025+SpringBoot3.5.0+gateway+webflux子服务路由报503