Thread
The system supports pthread
threads. If you know the pthread
interface, you can also use the posix
interface to implement threads.
In addition, we also provide a wrapper class for pthread
. This category includes the following three parts
- Thread.h:Thread class
- Mutex.h:Lock class
- Condition.h:Condition variable class
Use of threads
Introduce the header file, inherit the
Thread
class, implement thevirtual bool threadLoop()
function, and choose to implement the readyToRun function as needed.#include <system/Thread.h> class MyThread: public Thread { public: /** * After the thread is created successfully, this function will be called, and some initialization operations can be done in this function * return true Continue thread * false Exit thread */ virtual bool readyToRun() { LOGD("Thread Has been created"); return true; } /** * Thread loop function * * return true Continue thread loop * false Launch thread */ virtual bool threadLoop() { LOGD("Thread loop function"); //Check if there is a request to exit the thread, if so, return false and exit the thread immediately if (exitPending()) { return false; } //Accumulate the count and display it on the screen loop_count += 1; mTextView2Ptr->setText(loop_count); //To facilitate observation, add sleep 500ms here usleep(1000 * 500); //Return true, continue the next thread loop return true; } };
Instantiate thread object
static MyThread my_thread;
- Start thread
//The thread can be started by calling the run function of the thread class, //The parameter is the thread name, which can be arbitrarily specified. my_thread.run("this is thread name");
Stop thread The
Thread
class provides two functions for requesting to exit the thread. The difference between synchronous and asynchronous is used as follows:- void requestExitAndWait()
//Request to exit the thread, and wait until the thread completely exits, the function does not return my_thread.requestExitAndWait();
- void requestExit()
/*Request to exit the thread, the function returns immediately after the request is sent, but at this time, it does not mean that the thread has also exited */ my_thread.requestExit();
After calling any of the above two functions, in the
threadLoop
function, we can use thebool exitPending()
member function to determine whether there is a request to exit the thread.virtual bool threadLoop() { LOGD("Thread loop function"); //Check if there is a request to exit the thread, if so, return false and exit the thread immediately if (exitPending()) { return false; } return true; }
[!Note] Note that the above function just adds a mark requesting to exit the thread, not forcibly terminating the thread.
If you have been performing certain operations in thethreadLoop
function, and thethreadLoop
function has never been able to end, then the thread will not stop.
The correct approach is to check the thread exit request inthreadLoop
, or check a termination condition, and then returnfalse
.
[!Warning] It is forbidden to call the
requestExitAndWait
andrequestExit
functions in thethreadLoop
function, which may cause deadlock.- void requestExitAndWait()
Determine whether the thread is still running
if (my_thread.isRunning()) { mTextView4Ptr->setText("running"); } else { mTextView4Ptr->setText("stopped"); }
Thread flow chart
After the explanation of the above steps, combined with the flowchart, you should have a deeper understanding.
Sample code
See the ThreadDemo
project in the sample code