android thread使用测试

本文主要是想测试 threadLoop 线程是不是循环执行。


1. ThreadTest.cpp

#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>

#include <utils/threads.h>


namespace android {

    class ThreadTest : public Thread {
        virtual bool threadLoop();
    };

    bool ThreadTest::threadLoop() {
        printf("threadLoop\n");
        sleep(1);
        return true;
    }
}

using namespace android;

int main() {
    
    sp<ThreadTest> thr = new ThreadTest();
    thr->run();

    ProcessState::self()->startThreadPool();
    IPCThreadState::self()->joinThreadPool();

    return 0;
}

2. Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE_TAGS:= optional

LOCAL_SRC_FILES := \
    ThreadTest.cpp

LOCAL_SHARED_LIBRARIES := \
    libutils \
    libbinder

LOCAL_MODULE := ThreadTest

include $(BUILD_EXECUTABLE)

3. 执行

可以看到每隔1秒,打印一次threadLoop,所以threadLoop是循环执行。

若用指针代替android的sp,即StrongPointer,则只打印一次threadLoop。如下所示:

//sp<ThreadTest> thr = new ThreadTest();
ThreadTest *thr = new ThreadTest();

或者在threadLoop函数中return false,也只打印一次threadLoop。


原因是Thread类的实现中,

先由pthreads或win32_threads实现创建thread,并将线程入口设为_threadLoop。

_threadLoop再调用thread类实现者(ThreadTest)的threadLoop,并将这个threadLoop

放在do-while循环中,只有当threadLoop返回false或线程对象指针不是sp实现时,会退出循环。

_threadLoop示意代码:

int Thread::_threadLoop(void* user)
{
    Thread* const self = static_cast<Thread*>(user);

    sp<Thread> strong(self->mHoldSelf);
    wp<Thread> weak(strong);
    self->mHoldSelf.clear();

#ifdef HAVE_ANDROID_OS
    // this is very useful for debugging with gdb
    self->mTid = gettid();
#endif

    bool first = true;

    do {
        bool result;
        if (first) {
            first = false;
            self->mStatus = self->readyToRun();
            result = (self->mStatus == NO_ERROR);

            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 scope
            self->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 loop
        strong = weak.promote();
    } while(strong != 0);
    
    return 0;
}

关于sp,wp,可参考:

深入理解android常用类









  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值