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.