Natice层线程创建的方法

有两个方法在native层创建thread:

1.直接使用pthread_create函数

bionic/libc/bionic/pthread_create.cpp
注册的函数是怎样被调用到的?
int pthread_create(pthread_t* thread_out, pthread_attr_t const* attr,
                   void* (*start_routine)(void*), void* arg) {


  pthread_internal_t* thread = NULL;
  void* child_stack = NULL;
  int result = __allocate_thread(&thread_attr, &thread, &child_stack);




  thread->start_routine = start_routine;
  thread->start_routine_arg = arg;


  thread->set_cached_pid(getpid());




  int rc = clone(__pthread_start, child_stack, flags, thread, &(thread->tid), tls, &(thread->tid));


  int init_errno = __init_thread(thread);


  // Publish the pthread_t and unlock the mutex to let the new thread start running.
  *thread_out = __pthread_internal_add(thread);
  pthread_mutex_unlock(&thread->startup_handshake_mutex);


  return 0;
}


static int __pthread_start(void* arg) {
  pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(arg);


  void* result = thread->start_routine(thread->start_routine_arg);
  pthread_exit(result);


  return 0;
}


线程的名字是怎样被更改的?默认是?
prctl(PR_SET_NAME, "xxxx");

2. 使用继承基类Thread的方法

方法1 创建thread还是比较简单的,直观的。但是下面这种方式,就不太直观
要找到基类Thread的实现,在哪里? 看头文件就行,通过头文件,和文件名等,最终找到
Thead基类的实现system/core/libutils/Threads.cpp
    class EventThread : public Thread {
    public:
        EventThread(const char* sourceName, int sourceType);


        virtual bool threadLoop();
        const char* getName(){ return mSourceName.string(); }
    private:
        int mSourceType;
        String8 mSourceName;
    };
从中可以看到,主干函数threadLoop是怎样被调到的。
monitor_thread->run(monitor_thread->getName(), PRIORITY_NORMAL);


status_t Thread::run(const char* name, int32_t priority, size_t stack)
{


    mThread = thread_id_t(-1);


    // hold a strong reference on ourself
    mHoldSelf = this;


    mRunning = true;


    if (mCanCallJava) {
        res = createThreadEtc(_threadLoop,
                this, name, priority, stack, &mThread);
    } else {
        res = androidCreateRawThreadEtc(_threadLoop,
                this, name, priority, stack, &mThread);
    }




    return NO_ERROR;
}


int androidCreateRawThreadEtc(android_thread_func_t entryFunction,
                               void *userData,
                               const char* threadName __android_unused,
                               int32_t threadPriority,
                               size_t threadStackSize,
                               android_thread_id_t *threadId)
{


    pthread_t thread;
    int result = pthread_create(&thread, &attr,
                    (android_pthread_entry)entryFunction, userData);


}


pthread_create创建线程的入口函数是已知的:_threadLoop
int Thread::_threadLoop(void* user)
{
    Thread* const self = static_cast<Thread*>(user);
    bool first = true;


    result = self->threadLoop();


    return 0;
}


self->threadLoop();这个就是子类生成的主函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值