Android C++ multithreading

Android C++ multithreading

Introduction to Android C++ Threads

Android is based on the Linux kernel, and the Linux kernel follows the POSIX thread standard. There are a series of Pthreads APIs in the POSIX thread library to facilitate us to operate Linux threads, so we use C/C++ threads in Android to use the POSIX thread library, they are all in the head In the file "pthread.h".

Create child thread

1. pthread_t : used to declare a thread object such as: pthread_t thread ;

2. pthread_mutex_init : used to initialize the pthread_mutex lock object such as: pthread_mutex_init(&mutex,NULL) ;

3. pthread_mutex_destroy : used to destroy pthread_mutex_t lock objects such as: pthread_mutex_destroy(&mutex) ;

4. pthread_cond_t : used to create thread condition objects such as: pthread_cond_t cond ;

5. pthread_cond_init : used to initialize pthread_cond_t condition objects such as: pthread_cond_init(&cond, NULL);

6. pthread_cond_destroy : used to destroy pthread_cond_t condition objects such as: pthread_cond_destroy(&cond) ;

7, pthread_mutex_lock : used to lock the mutex , other variables after the thread is locked cannot be operated by other threads, such as: pthread_mutex_lock(&mutex) ;

8. pthread_mutex_unlock : used to unlock mutex , other variables after unlocking can be operated by other threads such as: pthread_mutex_unlock(&mutex) ;

9. pthread_cond_signal : used to issue conditional signals such as: pthread_cond_signal(&mutex, &cond) ;

10. pthread_cond_wait : used for thread blocking and waiting, until pthread_cond_signal sends out the condition signal before executing the exit thread blocking to perform the subsequent operations.