新增:实现 ThreadBase 类,创建单个线程
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/build/libs/
|
||||
/build/obj/
|
||||
42
.vscode/settings.json
vendored
Normal file
42
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"iostream": "cpp",
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"cctype": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"optional": "cpp",
|
||||
"string": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"fstream": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"new": "cpp",
|
||||
"ostream": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"typeinfo": "cpp"
|
||||
}
|
||||
}
|
||||
28
build/Ndk_Android.mk
Normal file
28
build/Ndk_Android.mk
Normal file
@ -0,0 +1,28 @@
|
||||
# 设置当前模块的源文件路径
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
|
||||
# 清除几乎所有 LOCAL_XXX 变量,为定义新模块做准备
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
# 模块名称,这将决定输出文件的名字(例如生成 libmynative.so)
|
||||
LOCAL_MODULE := myapp
|
||||
|
||||
# 指定需要编译的源文件
|
||||
LOCAL_SRC_FILES := \
|
||||
../code/ThreadBase.cpp ../code/main.cpp
|
||||
|
||||
# 指定头文件搜索路径
|
||||
LOCAL_C_INCLUDES := \
|
||||
$(LOCAL_PATH)/../code/
|
||||
|
||||
# 编译C文件时的标志
|
||||
LOCAL_CFLAGS := -D__OS_ANDROID
|
||||
|
||||
# 编译C++文件时的标志
|
||||
LOCAL_CPPFLAGS += -std=c++11 -android
|
||||
|
||||
# 链接时依赖的系统库
|
||||
LOCAL_LDLIBS := -llog -lc
|
||||
|
||||
# 指示构建系统将其编译为动态链接库
|
||||
include $(BUILD_EXECUTABLE)
|
||||
4
build/Ndk_Application.mk
Normal file
4
build/Ndk_Application.mk
Normal file
@ -0,0 +1,4 @@
|
||||
APP_STL:=gnustl_static
|
||||
APP_ABI:=armeabi-v7a
|
||||
APP_PLATFORM:=android-23
|
||||
APP_BUILD_SCRIPT:=Ndk_Android.mk
|
||||
99
build/ndk_make.sh
Executable file
99
build/ndk_make.sh
Executable file
@ -0,0 +1,99 @@
|
||||
#!/bin/bash
|
||||
|
||||
source /opt/ndk/env/ndk_env.sh
|
||||
|
||||
# 定义颜色
|
||||
RED="\033[0;31m"
|
||||
GREEN="\033[0;32m"
|
||||
YELLOW="\033[1;33m"
|
||||
BLUE="\033[0;34m"
|
||||
RESET="\033[0m"
|
||||
|
||||
# 路径
|
||||
NDK_PROJECT_PATH=$(pwd)
|
||||
APP_MK="./Ndk_Application.mk"
|
||||
ANDROID_MK="./Ndk_Android.mk"
|
||||
FIRMWARE_PATH="obj/local/armeabi-v7a/myapp"
|
||||
DEVICE_DATA="/data/"
|
||||
DEVICE_IP=""
|
||||
|
||||
clean() {
|
||||
echo -e "${YELLOW}Cleaning up...${RESET}"
|
||||
ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=$APP_MK APP_BUILD_SCRIPT=$ANDROID_MK clean
|
||||
echo -e "${YELLOW}Removing obj/ and libs/ directories...${RESET}"
|
||||
rm -rf obj/ libs/
|
||||
sync
|
||||
echo -e "${GREEN}Cleaned up.${RESET}"
|
||||
}
|
||||
|
||||
build_only() {
|
||||
echo -e "${BLUE}Building myapp...${RESET}"
|
||||
ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=$APP_MK APP_BUILD_SCRIPT=$ANDROID_MK
|
||||
echo -e "${GREEN}Only build completed successfully.${RESET}"
|
||||
}
|
||||
|
||||
push_only() {
|
||||
if [ -z "$DEVICE_IP" ]; then
|
||||
echo -e "${RED}Error: No device IP provided. Use -p <IP>${RESET}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e "${BLUE}Connecting to device $DEVICE_IP...${RESET}"
|
||||
adb connect $DEVICE_IP
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "${RED}Failed to connect to device $DEVICE_IP${RESET}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo -e "${BLUE}Pushing myapp to $DEVICE_DATA...${RESET}"
|
||||
adb -s $DEVICE_IP push $FIRMWARE_PATH $DEVICE_DATA
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "${RED}Failed to push myapp to $DEVICE_DATA${RESET}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo -e "${GREEN}Push completed successfully.${RESET}"
|
||||
}
|
||||
|
||||
build_and_push() {
|
||||
build_only
|
||||
push_only
|
||||
}
|
||||
|
||||
show_help() {
|
||||
echo -e "${YELLOW}Usage: $0 [options]${RESET}"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo -e " ${BLUE}-c${RESET} Clean project (remove obj/ and libs/)"
|
||||
echo -e " ${BLUE}-p <IP>${RESET} Push myapp to device with given IP"
|
||||
echo -e " ${BLUE}-bp <IP>${RESET} Build and push to device with given IP"
|
||||
echo -e " ${BLUE}-h${RESET} Show this help message"
|
||||
echo
|
||||
echo -e "${YELLOW}Default:${RESET} If no options are given, only build the project."
|
||||
}
|
||||
|
||||
# 参数解析
|
||||
if [ $# -eq 0 ]; then
|
||||
build_only
|
||||
exit 0
|
||||
fi
|
||||
|
||||
case "$1" in
|
||||
-c)
|
||||
clean
|
||||
;;
|
||||
-p)
|
||||
DEVICE_IP="$2"
|
||||
push_only
|
||||
;;
|
||||
-bp)
|
||||
DEVICE_IP="$2"
|
||||
build_and_push
|
||||
;;
|
||||
-h)
|
||||
show_help
|
||||
;;
|
||||
*)
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
93
code/ThreadBase.cpp
Normal file
93
code/ThreadBase.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
#include "ThreadBase.h"
|
||||
|
||||
ThreadBase::ThreadBase(void)
|
||||
{
|
||||
p_tThreadCnt = 0;
|
||||
m_tThreadPid_ = 0;
|
||||
}
|
||||
|
||||
ThreadBase::~ThreadBase()
|
||||
{
|
||||
}
|
||||
|
||||
void *ThreadBase::ThreadBase_ThreadLoopDistribute(void *pInputArg)
|
||||
{
|
||||
ThreadParam *pTempParam = (ThreadParam *)pInputArg;
|
||||
ThreadBase *pTempThread = (ThreadBase *)(pTempParam->m_pInstance_);
|
||||
pTempThread->ThreadBase_ThreadLoopFunc(pTempParam->m_tFlag_, pTempParam->m_pArg_);
|
||||
free(pTempParam);
|
||||
std::cout << "ThreadExit 子线程已退出" << std::endl;
|
||||
pthread_detach(pthread_self());
|
||||
std::cout << "ThreadExit 子线程已分离" << std::endl;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void ThreadBase::ThreadBase_ThreadJoin(void)
|
||||
{
|
||||
if (m_tThreadPid_ != 0)
|
||||
pthread_join(m_tThreadPid_, NULL);
|
||||
}
|
||||
|
||||
int ThreadBase::ThreadBase_ThreadCreate(void)
|
||||
{
|
||||
return ThreadBase_InterCreateThread(-1, 0, NULL);
|
||||
}
|
||||
|
||||
int ThreadBase::ThreadBase_ThreadCreate(void *pInputArg)
|
||||
{
|
||||
|
||||
return ThreadBase_InterCreateThread(-1, 0, pInputArg);
|
||||
}
|
||||
|
||||
int ThreadBase::ThreadBase_ThreadCreate(int tInputFlag, void *pInputArg)
|
||||
{
|
||||
return ThreadBase_InterCreateThread(-1, tInputFlag, pInputArg);
|
||||
}
|
||||
|
||||
int ThreadBase::ThreadBase_ThreadCreateWithPrio(int tInputPrio)
|
||||
{
|
||||
return ThreadBase_InterCreateThread(tInputPrio, 0, NULL);
|
||||
}
|
||||
|
||||
int ThreadBase::ThreadBase_InterCreateThread(int tInputPrio, int tInputFlag, void *pInputArg)
|
||||
{
|
||||
ThreadParam *pTempParam;
|
||||
|
||||
pTempParam = (ThreadParam *)malloc(sizeof(ThreadParam));
|
||||
if (pTempParam == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
p_tThreadCnt++;
|
||||
pTempParam->m_pInstance_ = this;
|
||||
pTempParam->m_tFlag_ = tInputFlag;
|
||||
pTempParam->m_pArg_ = pInputArg;
|
||||
#ifdef dUseExternLib2
|
||||
if (tInputPrio >= 0 && MergeClient_GetChipType() == dMergeChipType_Rtd1296)
|
||||
{
|
||||
pthread_attr_t tTempAttr;
|
||||
struct sched_param tTempParam;
|
||||
int policy;
|
||||
|
||||
pthread_attr_init(&tTempAttr);
|
||||
|
||||
pthread_attr_getschedpolicy(&tTempAttr, &policy);
|
||||
if (policy != SCHED_RR)
|
||||
pthread_attr_setschedpolicy(&tTempAttr, SCHED_RR);
|
||||
|
||||
tTempParam.sched_priority = tInputPrio;
|
||||
pthread_attr_setschedparam(&tTempAttr, &tTempParam);
|
||||
pthread_create(&(pTempParam->m_tThreadID_), (tInputPrio >= 0) ? (&tTempAttr) : NULL, ThreadBase_ThreadLoopDistribute, pTempParam);
|
||||
}
|
||||
else
|
||||
#else
|
||||
(void)tInputPrio;
|
||||
#endif
|
||||
{
|
||||
pthread_create(&(pTempParam->m_tThreadID_), NULL, ThreadBase_ThreadLoopDistribute, pTempParam);
|
||||
}
|
||||
m_tThreadPid_ = pTempParam->m_tThreadID_;
|
||||
|
||||
return (int)pTempParam->m_tThreadID_;
|
||||
}
|
||||
38
code/ThreadBase.h
Normal file
38
code/ThreadBase.h
Normal file
@ -0,0 +1,38 @@
|
||||
#ifndef THREADBASE_H
|
||||
#define THREADBASE_H
|
||||
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
|
||||
class ThreadBase
|
||||
{
|
||||
public:
|
||||
ThreadBase(void);
|
||||
virtual ~ThreadBase();
|
||||
virtual void ThreadBase_ThreadLoopFunc(int tInputFlag, void *pInputArg) = 0;
|
||||
// virtual void ThreadBase_ThreadLoopFunc(int tInputFlag, void *pInputArg) {}
|
||||
static void *ThreadBase_ThreadLoopDistribute(void *pInputArg);
|
||||
void ThreadBase_ThreadJoin(void);
|
||||
int ThreadBase_ThreadCreate(void);
|
||||
int ThreadBase_ThreadCreate(void *pInputArg);
|
||||
int ThreadBase_ThreadCreate(int tInputFlag, void *pInputArg);
|
||||
int ThreadBase_ThreadCreateWithPrio(int tInputPrio);
|
||||
|
||||
private:
|
||||
struct ThreadParam
|
||||
{
|
||||
pthread_t m_tThreadID_;
|
||||
void *m_pInstance_;
|
||||
int m_tFlag_;
|
||||
void *m_pArg_;
|
||||
};
|
||||
pthread_t m_tThreadPid_;
|
||||
|
||||
int ThreadBase_InterCreateThread(int tInputPrio, int tInputFlag, void *pInputArg);
|
||||
|
||||
protected:
|
||||
int p_tThreadCnt;
|
||||
};
|
||||
|
||||
#endif
|
||||
33
code/main.cpp
Normal file
33
code/main.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "ThreadBase.h"
|
||||
|
||||
/* 定义一个继承类,重写 ThreadBase_ThreadLoopFunc */
|
||||
class MyThread : public ThreadBase
|
||||
{
|
||||
public:
|
||||
void ThreadBase_ThreadLoopFunc(int tInputFlag, void *pInputArg) override
|
||||
{
|
||||
std::string *pTempMSG = (std::string *)pInputArg;
|
||||
for (int tTempN = 0; tTempN < 5; tTempN++)
|
||||
{
|
||||
std::cout << "[子线程] flag=" << tInputFlag
|
||||
<< " pTempMSG=" << *pTempMSG
|
||||
<< " tTempN=" << tTempN << std::endl;
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
MyThread tTempThread;
|
||||
std::string tTempString = "HelloThread";
|
||||
|
||||
/* 启动线程 */
|
||||
std::cout << "[主线程] 创建线程..." << std::endl;
|
||||
tTempThread.ThreadBase_ThreadCreate(123, &tTempString);
|
||||
/* 等待子线程退出 */
|
||||
tTempThread.ThreadBase_ThreadJoin();
|
||||
std::cout << "[主线程] 线程已退出" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user