Pthread Learning for Linux and Android

Source: Internet
Author: User

At first, we wanted to write an example of the difference between Pthread_mutex_lock and Pthread_mutex_trylock. On the Linux machine soon over, but think about it, Pthread is a UNIX department, in Windows no way to directly run the code is very inconvenient. So think of Android,windows installed NDK, mobile root can run Pthread code ...

The difference between Demolock and Trylock is also well understood: the former is blocked, death knows the mutex is released, and the latter is more flexible, Scratch, make a try and do something else. The test code PT_LOCK.C is as follows:
#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 (&AMP;MT);p rintf ("i[tid=%u" Am using, (*| ^_^ |*) \ n ", (uint_t) tid), Sleep;p thread_mutex_unlock (&AMP;MT);}  void Try_lock_func (void* arg) {uint_t tid = (uint_t) pthread_self (); int counter = 0;while (Pthread_mutex_trylock (&AMP;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 (&AMP;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 (&AMP;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 (&AMP;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 for MT mutex, 3 thread is flexible, every 1 seconds to detect whether the Mt mutex can be used, will not block. 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 first mt,10s tid=1090525504 blocked, while the thread tid=1098918208 the MT availability per 1s test.
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 compile and run Pt_lock reference the previous blog:http://blog.csdn.net/ryfdizuo/article/details/28891649 specific batch run.cmd as follows:
@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 system library paths inside will need to be modified based on the actual location 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 under the Android-ndk-r9b-windows-x86\platforms\android-13\arch-arm\usr\lib directory need to be specified: Similar in android.mk. Command line run Run.cmd, compile Pt_lock, and then push to the phone, note that my NEXUS4 must be/data/local/tmp below have permission, run after error, specific 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, N4 installed on the Android L system, GG know the need to compile the link to add pie and other settings, see [Ref2],fpie is the compile time option, pie is the link when the option, re-compile, push to the phone run OK ... PT_LOCK.C does not need to make any changes.
Refer

1. The pie and Fpie option flags below GCC are explained, 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

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.