At first, I wanted to write a demo sample to test the difference between Pthread_mutex_lock and Pthread_mutex_trylock. On the Linux machine very quickly over, but thought for a moment. Pthread is a UNIX department, and it's not easy to execute code directly in Windows. So think of Android,windows installed NDK, mobile root can run Pthread code ...
The difference between Demolock and Trylock is also very well understood: the former is clogged. Death know that mutually exclusive locks are released, while the latter is more flexible. Scratch。 Try not to do anything else to do. Test code PT_LOCK.C such as the following:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include < unistd.h> #include <sys/types.h>typedef pthread_t pt_t;typedef unsigned int uint_t;pthread_mutex_t mt = Pthread _mutex_initializer;void Lock_func (void* Arg) {pid_t pid;//processpt_t tid;//threadpid = Getpid (); tid = Pthread_self (); printf ("Want to lock mutex, msg=%s, tid=%u\n", (char*) arg, (uint_t) tid);p Thread_mutex_lock (&MT);p rintf ("i[tid=%u" Am using, (*| ^_^ |*) \ n ", (uint_t) tid), Sleep;p thread_mutex_unlock (&MT);} void Try_lock_func (void* arg) {uint_t tid = (uint_t) pthread_self (); int counter = 0;while (Pthread_mutex_trylock (&MT) {sleep (1); ++counter;printf ("After sleep 1s, I [tid=%u] want to try again, iter=%d.\n", Tid, counter);} printf ("It is my[tid=%u] turn, so long I waited...msg=%s\n", Tid, (char*) arg);p Thread_mutex_unlock (&MT);} #define XX_CREATE_FAILED (Err) printf ("CREATE thread Error:%s\n", strerror (Err)); return 1;int main () {int rc;pt_tPT1, Pt2, pt3;const char* msg1 = "block"; Const char* MSG2 = "unblock"; rc = Pthread_create (&PT1, NULL, (void*) &lock _func, (void*) MSG1); if (rc! = 0) {xx_create_failed (RC);} rc = Pthread_create (&pt2, NULL, (void*) &lock_func, (void*) MSG1); if (rc! = 0) {xx_create_failed (RC);} Sleep (1); rc = Pthread_create (&PT3, NULL, (void*) &try_lock_func, (void*) MSG2); if (rc! = 0) {xx_create_failed (RC) ;} Pthread_join (PT1, NULL);p thread_join (pt2, NULL);p thread_join (PT3, null); return 0;}The code idea is also very well understood: create three threads. 1 and 2 through the lock way to scramble MT Mutual exclusion lock, 3 thread is flexible, every 1 seconds to detect the MT mutual exclusion lock can be enough, will not clog.
Threads 1 or 2 occupy MT for a period of 10 seconds.
Linux Run
The output is consistent with expectations, and the id=1082132800 thread occupies Mt first. 10s internal thread tid=1090525504 blocked, while threads tid=1098918208 every 1s test for MT availability.
Want to lock mutexes, Msg=block, tid=1082132800i[tid=1082132800] am using, (*| ^_^ |*) want to lock mutex, Msg=block, tid=10905 25504after sleep 1s, I [tid=1098918208] want to try again, iter=1.after sleep 1s, I [tid=1098918208] want to try again, it Er=2.after sleep 1s, I [tid=1098918208] want to try again, iter=3.after sleep 1s, I [tid=1098918208] want to try again, it Er=4.after sleep 1s, I [tid=1098918208] want to try again, iter=5.after sleep 1s, I [tid=1098918208] want to try again, it Er=6.after sleep 1s, I [tid=1098918208] want to try again, iter=7.after sleep 1s, I [tid=1098918208] want to try again, it ER=8.I[TID=1090525504] am using, (*| ^_^ |*) after sleep 1s, I [tid=1098918208] want to try again, iter=9.after sleep 1s, I [ TID=1098918208] Want to try again, iter=10.after sleep 1s, I [tid=1098918208] want to try again, iter=11.after sleep 1s, i [tid=1098918208] Want to try again, iter=12.after sleep 1s, I [tid=1098918208] want to try again, iter=13.after sleep 1s, I [tid=1098918208] Want To try again, iter=14.after sleep 1s, I [tid=1098918208] want to try again, iter=15.after sleep 1s, I [tid=1098918208] wa NT to try again, iter=16.after sleep 1s, I [tid=1098918208] want to try again, iter=17.after sleep 1s, i [tid=1098918208] Want to try again, iter=18.after sleep 1s, I [tid=1098918208] want to try again, iter=19.it are my[tid=1098918208] turn, so Long I Waited...msg=unblock
Android Runandroid compiled and executed pt_lock previous article blog:http://blog.csdn.net/ryfdizuo/article/details/28891649 Detailed batch processing run.cmd such as the following:
@echo "1. Build. o File "@Rem fpie flag is used in compiling stage. D:\android-ndk-r9b-windows-x86\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\ Arm-linux-androideabi-gcc.exe--sysroot=d:\android-ndk-r9b-windows-x86\platforms\android-13\arch-arm-fpie-c Pt_ Lock.c@echo "2. Build exec file "@Rem Pie flag is used in linking stage. D:\android-ndk-r9b-windows-x86\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\ Arm-linux-androideabi-gcc.exe--sysroot=d:\android-ndk-r9b-windows-x86\platforms\android-13\arch-arm-pie-o Pt_ Lock Pt_lock.o@pause
The GCC and Android library paths inside are subject to actual location changes on the machine.Note that the GCC compilation on Android is a little bit different from Linux:GCC does not need to explicitly specify-lpthread in the NDK. Pthread may be linked by default.
These so loads in the Android-ndk-r9b-windows-x86\platforms\android-13\arch-arm\usr\lib folder need to be specified. Similar in android.mk. Command line execution run.cmd, compile Pt_lock, and then push to the phone, note that my NEXUS4 must be/data/local/tmp below to have permission, after execution error, detailed steps:
E:\GITHUB\NDK_TUTORIAL\PTHREAD_PLAYGROUND>ADB Shell[email protected]:/# cd/data/local/tmpcd/data/local/tmp[ Email protected]:/data/local/tmp # mv/sdcard/pt_lock/mv/sdcard/pt_lock./[email protected]:/data/local/tmp # chmod 75 1 Pt_lockchmod 751 Pt_lock[email protected]:/data/local/tmp #./pt_lockerror:only position independent executables (PIE) is supported.
Reported that PIE does not support the error, the N4 is installed on the Android L system. GG after know to compile the link need to add pie and other settings, see [Ref2],fpie is the compile time option, pie is the link when the option. Compile again, push to the phone to execute OK ... PT_LOCK.C do not need to do whatever changes.
Pthread Learning1. Thread-related1) callback function type void* (*PFUNC) (void* Arg). The parameters are passed through the void*, and multiple parameters are packaged through the struct/array. 2) thread properties, the ability to set the thread type, usually joinable. Sets the size of the line stacks. Assume that the thread inside to define a large array must be careful to stack out of bounds.
3) thread termination in several cases:
- The callback function finishes executing. Normal exit
- The Pthread_exit function is called within the thread. Quit whether or not you are finished. All of the code behind the function will not be executed (especially the statements such as printf).
- Other lines Cheng call Pthread_cancel to end the current thread.
- The entire process is terminated because functions such as exit are called. All the threads below it are killed. The resource is recycled.
- The main function creates n threads. The two practices wait for the other thread to end: an explicit pthread_join waits for the other thread to end, and the main function calls Pthread_exit, waiting for the other thread to end.
2. Mutual exclusion Lock-related
Pthread_mutex_initpthread_mutex_lock. Blocking wait
Pthread_mutex_trylock, non-clogging wait
Pthread_mutex_destroy
3. Semaphore CorrelationPthread_mutex_initpthread_mutex_wait block the current thread until certain conditions are met. Pseudo-code such as the following:BeginPthread_mutex_unlock (mutex) is blocked before releasing mutually exclusive locksBlock_on_cond (condition) condition blockagePthread_mutex_lock (mutex) conditions after the possession of the mutually exclusive lock, after the completion of work need to release mutually exclusive lockEnd the statement must run in a loop, not if, because there may be spurious wake behavior between threads, spurious wakeup. When awakened, it is necessary to determine again whether the expected conditions are satisfied. Assume that the wait is not satisfied to continue.
Related discussions: http://a-shi.org/blog/2012/07/27/pthread_cond_wait_mutex_while/
Pthread_mutex_signal, only one thread waits when the semaphore is sent
Pthread_mutex_broadcast multiple threads waiting at the same time
Pthread_mutex_destroy
4. Survivor/Consumer, read and write. The Philosopher's Dining model Http://www.cs.nmsu.edu/~jcook/Tools/pthreads/examples.htmlrefer
1. The pie and Fpie option flags are explained below GCC, http://richardustc.github.io/blog/2013/05/pie/
2. Pie Error in Android L system, http://blog.csdn.net/hxdanya/article/details/39371759
3. Pthread http://man7.org/linux/man-pages/man3/pthread_create.3.html
Pthread Learning for Linux and Android