android8.1环境下 android Threads线程实现

Android native的Thread类是Android提供的一个基础类,源码路径:
system\core\libutils\include\utils\Thread.h
system\core\libutils\Threads.cpp

该类提供的基础功能涵盖了线程的生命周期:创建、运行、销毁。主要成员函数如下:
0、本身继承于RefBase,所以具有相应的一些特性
// Invoked after creation of initial strong pointer/reference.
    virtual void            onFirstRef();

1、执行线程创建并启动运行,通过run方法(和java有点差异):
status_t run(const char* name, int32_t priority, size_t stack);
2、循环执行方法
2.1 先执行readyToRun()
2.2 创建完成后,开始执行_threadLoop()函数,该函数主要通过调用threadLoop()函数,因此基类必要要实现threadLoop函数,作为线程执行函数,它是有返回值的方法,而且_threadLoop会根据返回值确定是否继续循环执行的方法。
3、线程请求退出方法
线程销毁,子类最好通过实现requestExit()函数,首先调用Thread类的requestExit()函数,将线程状态mExitPending置为true,然后中断threadLoop

线程优先级请参考https://blog.csdn.net/caonima0001112/article/details/50379738

android_thread.h

//
// Created by godv on 21-5-12.
//
#define LOG_TAG "godv_thread"
#ifndef ANDROID_ANDROID_THREAD_H
#define ANDROID_ANDROID_THREAD_H

#include 
//sleep的头文件
#include "unistd.h"

namespace android {
class android_thread : public Thread{
public:
	android_thread();
    virtual void        onFirstRef();
    virtual status_t    readyToRun();

	//如果返回true,循环调用此函数,返回false下一次不会再调用此函数
	virtual bool threadLoop();
	virtual void requestExit();
private:
 int hasRunCount = 0;
};
}



#endif //ANDROID_ANDROID_THREAD_H

android_thread.cpp

//
// Created by godv on 21-5-12.
//

#include "inc/android_thread.h"

#include 

namespace android {

	android_thread::android_thread() :
			Thread(false) {
		ALOGD("android_thread");
	}

	bool android_thread::threadLoop() {
		ALOGD("threadLoop hasRunCount = %d",hasRunCount);
		hasRunCount++;
		sleep(1);
		if (hasRunCount == 200) {
			return false;
		}
		return true;
	}

	void android_thread::onFirstRef() {
		ALOGD("onFirstRef");
	}
	status_t android_thread::readyToRun() {
		ALOGD("readyToRun");
		return 0;
	}

	void android_thread::requestExit() {
        Thread::requestExit();

		ALOGD("requestExit");
	}
}

main.cpp

//
// Created by godv on 21-5-12.
//
#define LOG_TAG "godv_thread"
#include 
#include 
#include "android_thread.h"

using namespace android;

int main()
{
    int count = 0;
	sp  thread = new android_thread;

	thread->run("godv", PRIORITY_URGENT_DISPLAY);
	while(1) {
	   count++;
	   ALOGD("count = %d",count);
	   if (!thread->isRunning()) {
	       ALOGD("thread exit");
		   break;
	    }
       if(count == 20){
           ALOGD("main requestExit");
           thread->requestExit();
       }
       sleep(2);
	}
	ALOGD("main end");
	return 0;
}

Android.mk

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := android_thread

#LOCAL_SRC_FILES := $(call all-subdir-cpp-files)

LOCAL_SRC_FILES := android_thread.cpp \
                   main.cpp

LOCAL_SHARED_LIBRARIES := liblog \
                          libandroid_runtime \
                          libcutils \
                          libutils

LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc

include $(BUILD_EXECUTABLE)

include $(CLEAR_VARS)

LOCAL_MODULE := godv_thread

LOCAL_SRC_FILES := thread_posix.cpp

LOCAL_SHARED_LIBRARIES += liblog

LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc

include $(BUILD_EXECUTABLE)

log

05-13 00:41:58.527  2438  2438 D godv_thread: android_thread
05-13 00:41:58.527  2438  2438 D godv_thread: onFirstRef
05-13 00:41:58.527  2438  2438 D godv_thread: count = 1
05-13 00:41:58.527  2438  2439 D godv_thread: readyToRun
05-13 00:41:58.527  2438  2439 D godv_thread: threadLoop hasRunCount = 0
05-13 00:41:59.527  2438  2439 D godv_thread: threadLoop hasRunCount = 1
05-13 00:42:00.527  2438  2438 D godv_thread: count = 2
05-13 00:42:00.528  2438  2439 D godv_thread: threadLoop hasRunCount = 2
05-13 00:42:01.528  2438  2439 D godv_thread: threadLoop hasRunCount = 3
05-13 00:42:02.528  2438  2438 D godv_thread: count = 3
05-13 00:42:02.528  2438  2439 D godv_thread: threadLoop hasRunCount = 4
05-13 00:42:03.528  2438  2439 D godv_thread: threadLoop hasRunCount = 5
05-13 00:42:04.528  2438  2438 D godv_thread: count = 4
05-13 00:42:04.529  2438  2439 D godv_thread: threadLoop hasRunCount = 6
05-13 00:42:05.529  2438  2439 D godv_thread: threadLoop hasRunCount = 7
05-13 00:42:06.529  2438  2438 D godv_thread: count = 5
05-13 00:42:06.529  2438  2439 D godv_thread: threadLoop hasRunCount = 8
05-13 00:42:07.530  2438  2439 D godv_thread: threadLoop hasRunCount = 9
05-13 00:42:08.529  2438  2438 D godv_thread: count = 6
05-13 00:42:08.530  2438  2439 D godv_thread: threadLoop hasRunCount = 10
05-13 00:42:09.530  2438  2439 D godv_thread: threadLoop hasRunCount = 11
05-13 00:42:10.529  2438  2438 D godv_thread: count = 7
05-13 00:42:10.531  2438  2439 D godv_thread: threadLoop hasRunCount = 12
05-13 00:42:11.531  2438  2439 D godv_thread: threadLoop hasRunCount = 13
05-13 00:42:12.530  2438  2438 D godv_thread: count = 8
05-13 00:42:12.531  2438  2439 D godv_thread: threadLoop hasRunCount = 14
05-13 00:42:13.532  2438  2439 D godv_thread: threadLoop hasRunCount = 15
05-13 00:42:14.530  2438  2438 D godv_thread: count = 9
05-13 00:42:14.532  2438  2439 D godv_thread: threadLoop hasRunCount = 16
05-13 00:42:15.532  2438  2439 D godv_thread: threadLoop hasRunCount = 17
05-13 00:42:16.530  2438  2438 D godv_thread: count = 10
05-13 00:42:16.532  2438  2439 D godv_thread: threadLoop hasRunCount = 18
05-13 00:42:17.533  2438  2439 D godv_thread: threadLoop hasRunCount = 19
05-13 00:42:18.531  2438  2438 D godv_thread: count = 11
05-13 00:42:18.533  2438  2439 D godv_thread: threadLoop hasRunCount = 20
05-13 00:42:19.533  2438  2439 D godv_thread: threadLoop hasRunCount = 21
05-13 00:42:20.531  2438  2438 D godv_thread: count = 12
05-13 00:42:20.534  2438  2439 D godv_thread: threadLoop hasRunCount = 22
05-13 00:42:21.534  2438  2439 D godv_thread: threadLoop hasRunCount = 23
05-13 00:42:22.531  2438  2438 D godv_thread: count = 13
05-13 00:42:22.534  2438  2439 D godv_thread: threadLoop hasRunCount = 24
05-13 00:42:23.535  2438  2439 D godv_thread: threadLoop hasRunCount = 25
05-13 00:42:24.532  2438  2438 D godv_thread: count = 14
05-13 00:42:24.535  2438  2439 D godv_thread: threadLoop hasRunCount = 26
05-13 00:42:25.536  2438  2439 D godv_thread: threadLoop hasRunCount = 27
05-13 00:42:26.532  2438  2438 D godv_thread: count = 15
05-13 00:42:26.536  2438  2439 D godv_thread: threadLoop hasRunCount = 28
05-13 00:42:27.536  2438  2439 D godv_thread: threadLoop hasRunCount = 29
05-13 00:42:28.532  2438  2438 D godv_thread: count = 16
05-13 00:42:28.536  2438  2439 D godv_thread: threadLoop hasRunCount = 30
05-13 00:42:29.537  2438  2439 D godv_thread: threadLoop hasRunCount = 31
05-13 00:42:30.532  2438  2438 D godv_thread: count = 17
05-13 00:42:30.537  2438  2439 D godv_thread: threadLoop hasRunCount = 32
05-13 00:42:31.537  2438  2439 D godv_thread: threadLoop hasRunCount = 33
05-13 00:42:32.532  2438  2438 D godv_thread: count = 18
05-13 00:42:32.537  2438  2439 D godv_thread: threadLoop hasRunCount = 34
05-13 00:42:33.538  2438  2439 D godv_thread: threadLoop hasRunCount = 35
05-13 00:42:34.533  2438  2438 D godv_thread: count = 19
05-13 00:42:34.538  2438  2439 D godv_thread: threadLoop hasRunCount = 36
05-13 00:43:31.329  2554  2554 D godv_thread: android_thread
05-13 00:43:31.329  2554  2554 D godv_thread: onFirstRef
05-13 00:43:31.329  2554  2554 D godv_thread: count = 1
05-13 00:43:31.329  2554  2555 D godv_thread: readyToRun
05-13 00:43:31.329  2554  2555 D godv_thread: threadLoop hasRunCount = 0
05-13 00:43:32.329  2554  2555 D godv_thread: threadLoop hasRunCount = 1
05-13 00:43:33.329  2554  2554 D godv_thread: count = 2
05-13 00:43:33.330  2554  2555 D godv_thread: threadLoop hasRunCount = 2
05-13 00:43:34.330  2554  2555 D godv_thread: threadLoop hasRunCount = 3
05-13 00:43:35.330  2554  2554 D godv_thread: count = 3
05-13 00:43:35.330  2554  2555 D godv_thread: threadLoop hasRunCount = 4
05-13 00:43:36.330  2554  2555 D godv_thread: threadLoop hasRunCount = 5
05-13 00:43:37.330  2554  2554 D godv_thread: count = 4
05-13 00:43:37.331  2554  2555 D godv_thread: threadLoop hasRunCount = 6
05-13 00:43:38.331  2554  2555 D godv_thread: threadLoop hasRunCount = 7
05-13 00:43:39.330  2554  2554 D godv_thread: count = 5
05-13 00:43:39.331  2554  2555 D godv_thread: threadLoop hasRunCount = 8
05-13 00:43:40.331  2554  2555 D godv_thread: threadLoop hasRunCount = 9
05-13 00:43:41.330  2554  2554 D godv_thread: count = 6
05-13 00:43:41.332  2554  2555 D godv_thread: threadLoop hasRunCount = 10
05-13 00:43:42.332  2554  2555 D godv_thread: threadLoop hasRunCount = 11
05-13 00:43:43.331  2554  2554 D godv_thread: count = 7
05-13 00:43:43.332  2554  2555 D godv_thread: threadLoop hasRunCount = 12
05-13 00:43:44.333  2554  2555 D godv_thread: threadLoop hasRunCount = 13
05-13 00:43:45.331  2554  2554 D godv_thread: count = 8
05-13 00:43:45.333  2554  2555 D godv_thread: threadLoop hasRunCount = 14
05-13 00:43:46.334  2554  2555 D godv_thread: threadLoop hasRunCount = 15
05-13 00:43:47.332  2554  2554 D godv_thread: count = 9
05-13 00:43:47.334  2554  2555 D godv_thread: threadLoop hasRunCount = 16
05-13 00:43:48.334  2554  2555 D godv_thread: threadLoop hasRunCount = 17
05-13 00:43:49.332  2554  2554 D godv_thread: count = 10
05-13 00:43:49.335  2554  2555 D godv_thread: threadLoop hasRunCount = 18
05-13 00:43:50.335  2554  2555 D godv_thread: threadLoop hasRunCount = 19
05-13 00:43:51.332  2554  2554 D godv_thread: count = 11
05-13 00:43:51.335  2554  2555 D godv_thread: threadLoop hasRunCount = 20
05-13 00:43:52.336  2554  2555 D godv_thread: threadLoop hasRunCount = 21
05-13 00:43:53.333  2554  2554 D godv_thread: count = 12
05-13 00:43:53.336  2554  2555 D godv_thread: threadLoop hasRunCount = 22
05-13 00:43:54.337  2554  2555 D godv_thread: threadLoop hasRunCount = 23
05-13 00:43:55.333  2554  2554 D godv_thread: count = 13
05-13 00:43:55.337  2554  2555 D godv_thread: threadLoop hasRunCount = 24
05-13 00:43:56.337  2554  2555 D godv_thread: threadLoop hasRunCount = 25
05-13 00:43:57.334  2554  2554 D godv_thread: count = 14
05-13 00:43:57.338  2554  2555 D godv_thread: threadLoop hasRunCount = 26
05-13 00:43:58.338  2554  2555 D godv_thread: threadLoop hasRunCount = 27
05-13 00:43:59.334  2554  2554 D godv_thread: count = 15
05-13 00:43:59.339  2554  2555 D godv_thread: threadLoop hasRunCount = 28
05-13 00:44:00.339  2554  2555 D godv_thread: threadLoop hasRunCount = 29
05-13 00:44:01.335  2554  2554 D godv_thread: count = 16
05-13 00:44:01.339  2554  2555 D godv_thread: threadLoop hasRunCount = 30
05-13 00:44:02.339  2554  2555 D godv_thread: threadLoop hasRunCount = 31
05-13 00:44:03.335  2554  2554 D godv_thread: count = 17
05-13 00:44:03.340  2554  2555 D godv_thread: threadLoop hasRunCount = 32
05-13 00:44:04.340  2554  2555 D godv_thread: threadLoop hasRunCount = 33
05-13 00:44:05.335  2554  2554 D godv_thread: count = 18
05-13 00:44:05.340  2554  2555 D godv_thread: threadLoop hasRunCount = 34
05-13 00:44:06.341  2554  2555 D godv_thread: threadLoop hasRunCount = 35
05-13 00:44:07.335  2554  2554 D godv_thread: count = 19
05-13 00:44:07.341  2554  2555 D godv_thread: threadLoop hasRunCount = 36
05-13 00:44:08.342  2554  2555 D godv_thread: threadLoop hasRunCount = 37
05-13 00:44:09.336  2554  2554 D godv_thread: count = 20
05-13 00:44:09.336  2554  2554 D godv_thread: main requestExit
05-13 00:44:09.336  2554  2554 D godv_thread: requestExit
05-13 00:44:11.336  2554  2554 D godv_thread: count = 21
05-13 00:44:11.336  2554  2554 D godv_thread: thread exit
05-13 00:44:11.336  2554  2554 D godv_thread: main end
 

你可能感兴趣的:(多线程,c++,android,thread)