Android thread Encapsulation

Source: Internet
Author: User

The specific implementation is not discussed here. The specific implementation isSystem


Associated ~~ First, anroid provides several functions for directly creating threads:
Inline bool createthread (thread_func_t F, void *)

Inline bool createthreadetc (thread_func_t entryfunction,
Void * userdata,
Const char * threadname =
"Android: unnamed_thread ",
Int32_t threadpriority = priority_default,
Size_t threadstacksize = 0,
Thread_id_t * threadid = 0)

Inline thread_id_t getthreadid ()

Let's take a look at the mutex of Android, which is basically similar to the mutex of POSIX. The only mutex: autolock is added. This automatic lock is used a lot.
Apply the lock in the scope, and the unlock will automatically go out of the scope.
Class autolock {
Public:
Inline autolock (mutex & mutex): mpmutex (& mutex ){
Mutex. Lock ();}
Inline autolock (mutex * mutex): mpmutex (mutex ){
Mutex-> lock ();}
Inline ~ Autolock () {mpmutex-> unlock ();}
PRIVATE:
Mutex * mpmutex;
};

Let's take a look at the andorid's condition. Its usage is basically the same as POSIX, because it is a conditional variable, there is only one mutex parameter ~~

Finally, let's take a look at the android Thread class. In actual use, we inherit this thread class to create our own Thread class and define the thread execution content.
Let's talk about the functions that need to be implemented to create your own Thread class:

Class thread: virtual public refbase
First, it inherits from the refbase class. Generally, You need to implement the onfirstref () parent class function. GenerallyClassic


Is to run the run function of the thread in it, so that the thread '% Ca % B5 % C0 % FD' is created); "href =" javascript :; "Target =" _ Self ">Instance


It starts to run this thread. Of course, you can also execute the run () function here and execute the run () function elsewhere to start this thread.

Virtual status_t run (const char * name = 0,
Int32_t priority = priority_default,
Size_t stack = 0 );
When a thread instance is created, the thread does not run. The thread starts to run only when the run () function is executed.

Virtual status_t readytorun ();
This function defines initialization before thread execution

Virtual bool threadloop () = 0;
This function is implemented by every Thread class. The thread execution content is defined here. If this function returns true, the function will continuously execute the content in threadloop. If
If this function returns false, the content in threadloop will exit after only one thread is executed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.