On the thread encapsulation _android in Android

Source: Internet
Author: User
Tags mutex posix thread class

Simply write Android's C + + encapsulation of threads ~ ~ ~ Actually the API has been written very clearly ~ ~

Encapsulated files:/frameworks/base/include/utils/threads.h

The implementation is not discussed here, and the implementation is related to the system ~ ~ First Anroid provides several functions that directly create a thread:

Copy Code code as follows:

inline bool CreateThread (thread_func_t F, void *a)

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 ()


Here's a look at the Android mutex, which is very similar to the POSIX mutex, the only addition to a mutex::autolock, this automatic lock is more, in the scope of the lock, the break out of the scope will be automatically unlocked.
Copy Code code as follows:

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;
};

Then look at the Andorid condition, which is basically the same as POSIX, because it's a conditional variable, so there's only one mutex parameter ~ ~

Finally look at the Android thread class, in the actual use of the process is to inherit the thread class to create their own thread class, and define the execution of the thread, the following mainly around the creation of their own thread classes need to implement several functions to say:

Class Thread:virtual Public Refbase
First of all, it inherits from the Refbase class, generally in the use of the time to implement the Onfirstref () the parent function, the general classic use is to run the thread in the run function, so that when creating an instance of thread to start running this thread. Of course, you can also not execute the run () function here, and execute the run () function elsewhere to start the thread.

Copy Code code as follows:

Virtual status_t Run (const char* name = 0,
int32_t priority = Priority_default,
size_t stack = 0);

When you create a thread instance, the thread does not run, and only when the run () function is executed does the thread begin to actually start running.

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

virtual bool Threadloop () = 0;
This function is implemented by each thread class, where the execution of thread is defined, and if this function returns True, then the function executes the contents of the Threadloop, if the function returns False, The contents of the Threadloop will exit only once the thread is executed once.

Related Article

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.