本文起初主要想写个演示样例实測下pthread_mutex_lock和pthread_mutex_trylock差别。在linux机器上非常快就over了,可是想了一下。pthread是unix系的,在windows没办法直接执行代码非常不方便。于是想到了android,windows上安装ndk,手机root就能够跑pthread代码咯。。。

demo

lock和trylock的差别也非常好理解:,前者是堵塞的。死等知道相互排斥锁被释放;而后者则更加灵活。浅尝辄止。做个尝试不行则干其它事情去。。測试代码pt_lock.c例如以下:
#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; //process
pt_t tid; // thread pid = getpid();
tid = pthread_self(); printf("want to lock mutex, msg=%s, tid=%u\n", (char*)arg, (uint_t)tid); pthread_mutex_lock( &mt );
printf("I[tid=%u] am using, (*|^_^|*)\n", (uint_t)tid);
sleep(10);
pthread_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);
pthread_mutex_unlock( &mt );
} #define XX_CREATE_FAILED(err) \
printf("create thread error : %s\n", strerror(err));\
return 1; int main()
{
int rc;
pt_t pt1, 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);
pthread_join(pt2, NULL);
pthread_join(pt3, NULL);
return 0;
}

代码思路也非常好理解:创建三个线程。1和2通过lock方式去争抢mt相互排斥锁,3线程则灵活,每隔1秒去检測下mt相互排斥锁能否够用,不会堵塞。

线程1或者2占有mt的时间为10秒。


linux run

xx@h10-1-152-72:~/peteryfren/cpp/pthread> gcc -Wall -o pt_lock pt_lock.c -lpthread
xx@h10-1-152-72:~/peteryfren/cpp/pthread> ./pt_lock

输出结果与预期一致,id=1082132800线程先占有mt。10s内线程tid=1090525504堵塞,而线程tid=1098918208每隔1s測试下mt可用性。

want to lock mutex, msg=block, tid=1082132800
I[tid=1082132800] am using, (*|^_^|*)
want to lock mutex, msg=block, tid=1090525504
after sleep 1s, i [tid=1098918208] want to try again, iter=1.
after sleep 1s, i [tid=1098918208] want to try again, iter=2.
after sleep 1s, i [tid=1098918208] want to try again, iter=3.
after sleep 1s, i [tid=1098918208] want to try again, iter=4.
after sleep 1s, i [tid=1098918208] want to try again, iter=5.
after sleep 1s, i [tid=1098918208] want to try again, iter=6.
after sleep 1s, i [tid=1098918208] want to try again, iter=7.
after sleep 1s, i [tid=1098918208] want to try again, iter=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] want 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 is my[tid=1098918208] turn, so long i waited...msg=unblock

android run

android上编译和执行pt_lock參考前一篇blog:http://blog.csdn.net/ryfdizuo/article/details/28891649   详细批处理run.cmd例如以下:
@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

里面的gcc和android系统库路径须要依据机器上实际位置改动。

注意android上gcc编译与linux上有点点区别:ndk中gcc不须要须要显式指定-lpthread。可能pthread默认就会链接。

android-ndk-r9b-windows-x86\platforms\android-13\arch-arm\usr\lib文件夹下的这些so载入须要指定。。在Android.mk中类似。

命令行下执行run.cmd,编译pt_lock,然后推到手机上,注意我的nexus4必须是/data/local/tmp以下才有权限,执行后有错误,详细步骤:
E:\GitHub\ndk_tutorial\pthread_playground>adb shell
root@mako:/ # cd /data/local/tmp
cd /data/local/tmp
root@mako:/data/local/tmp # mv /sdcard/pt_lock ./
mv /sdcard/pt_lock ./
root@mako:/data/local/tmp # chmod 751 pt_lock
chmod 751 pt_lock
root@mako:/data/local/tmp # ./pt_lock
error: only position independent executables (PIE) are supported.

报了PIE不支持的错误,n4上安装的是android L系统。gg后知道编译的链接的须要添加pie等设置,參见[ref2],fPIE是编译时候的选项,pie是链接时候的选项。再次又一次编译,推到手机上执行OK。。。pt_lock.c不须要做不论什么改动。


pthread学习

1.线程相关
1)回调函数类型 void* (*pfunc) (void* arg) 。參数通过void*传入,多个參数是通过结构体/数组打包。
2)线程属性,能够设置线程类型,通常是joinable。设置线程栈的大小。假设线程内部要定义大数组一定小心栈越界。

3)线程终止的几种情况:
  • 回调函数执行完毕。正常退出
  • 线程内调用pthread_exit函数。无论是否完毕都退出。该函数后面的全部代码都不会被执行(尤其printf等语句)。
  • 其它线程显式调用pthread_cancel 结束当前线程。
  • 整个进程被终止,由于调用了exit等函数。它以下的全部线程都被杀死。资源被回收。

  • main函数创建N个线程。两种做法等待其它线程结束:显式pthread_join等待其它线程结束;main函数中调用pthread_exit,等待其它线程结束。

2. 相互排斥锁相关
pthread_mutex_init
pthread_mutex_lock。堵塞等待
pthread_mutex_trylock,非堵塞等待
pthread_mutex_destroy


3. 信号量相关
pthread_mutex_init
pthread_mutex_wait 堵塞当前线程,直到满足某些条件时。伪代码例如以下:
begin
pthread_mutex_unlock( mutex )  堵塞之前释放相互排斥锁
block_on_cond( condition ) 条件堵塞
pthread_mutex_lock(mutex) 条件满足后 占有相互排斥锁,完毕工作后须要释放相互排斥锁
end
该语句必须在循环中运行,而不是if中,由于线程之间可能有虚假的唤醒行为,spurious wakeup。当唤醒时须要再次确定预期的条件是否满足。假设不满足继续等待。

相关的讨论參见:http://a-shi.org/blog/2012/07/27/pthread_cond_wait_mutex_while/


pthread_mutex_signal,仅仅有一个线程等待时发送信号量
pthread_mutex_broadcast 多个线程同一时候等待时使用
pthread_mutex_destroy

4. 生存者/消费者,读写。哲学家进餐模型

refer

1. gcc以下的pie和fPIE 选项标志解说,http://richardustc.github.io/blog/2013/05/pie/

2. android L系统下PIE错误,http://blog.csdn.net/hxdanya/article/details/39371759

3. pthread http://man7.org/linux/man-pages/man3/pthread_create.3.html

linux和android端的pthread学习的更多相关文章

  1. 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序(老罗学习笔记3)

    简单来说,硬件驱动程序一方面分布在Linux内核中,另一方面分布在用户空间的硬件抽象层中.接着,在Ubuntu上为Android系统编写Linux内核驱动程序(老罗学习笔记1)一文中举例子说明了如何在 ...

  2. webrtc学习笔记2(Android端demo代码结构)

    最近正在修改webrtc的Android端demo和服务器交互的内容,介绍一下demo的大体结构吧,以便能快速回忆. 环境:Android5.0以上.libjingle_peerconnection_ ...

  3. Ionic3学习笔记(九)关于 Android 端软键盘弹出后界面被压缩的问题

    本文为原创文章,转载请标明出处 今天做了一个如下图所示的页面. iOS 端毫无 bug,Android 端却出现了问题.当软键盘弹出后,Android 端的 tabs 移到了软键盘的上面,再仔细一看, ...

  4. Ionic3学习笔记(八)使iOS端、Android端 Navbar 透明化

    本文为原创文章,转载请标明出处 若要使iOS端 Navbar 透明,只需要给 ion-navbar 添加 transparent 属性,但是Android端依旧会有一条border在,所以再给 ion ...

  5. Android(java)学习笔记160:Framework运行环境之 Android进程产生过程

    1.前面Android(java)学习笔记159提到Dalvik虚拟机启动初始化过程,就下来就是启动zygote进程: zygote进程是所有APK应用进程的父进程:每当执行一个Android应用程序 ...

  6. 浅谈 unix, linux, ios, android 区别和联系

    浅谈 unix, linux, ios, android 区别和联系 网上的答案并不是很好,便从网上整理的相对专业的问答,本人很菜,大佬勿喷 UNIX 和 Linux   UNIX 操作系统(尤尼斯) ...

  7. Android(java)学习笔记103:Framework运行环境之 Android进程产生过程

    1. 前面Android(java)学习笔记159提到Dalvik虚拟机启动初始化过程,就下来就是启动zygote进程: zygote进程是所有APK应用进程的父进程:每当执行一个Android应用程 ...

  8. Android 开源项目及其学习

    Android 系统研究:http://blog.csdn.net/luoshengyang/article/details/8923485 Android 腾讯技术人员博客 http://hukai ...

  9. 【Android端APP 安装包检查】安装包检查具体内容及实现方法

    一.安装包检查的具体包含内容有哪些? 1.安装包检查的一般内容包括: 安装包基本信息检查: 文件大小: xx MB 包名: com.xx 名称:  xx 本次安装包证书与外网证书对比一致性:是 版本号 ...

随机推荐

  1. vs2012升级到vs2013后,sql server 无法通过IP登录解决方案

    因项目需要,vs升级到2013,可是数据库通过IP却无法连接,一直报内存异常,困扰许久,原来是.net框架出了问题,可以通过用管理员身份运行:netsh winsock reset解决!

  2. 基于paramiko进行远程执行Linux命令

    直接贴一段代码import paramiko class remote_start(object): def __init__(self,host,username,pwd): self.ssh = ...

  3. 算法笔记_166:算法提高 金属采集(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 人类在火星上发现了一种新的金属!这些金属分布在一些奇怪的地方,不妨叫它节点好了.一些节点之间有道路相连,所有的节点和道路形成了一棵树.一共 ...

  4. 算法笔记_098:蓝桥杯练习 算法提高 盾神与条状项链(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 有一天,盾神捡到了好多好多五颜六色的珠子!他心想这些珠子这么漂亮,可以做成一条项链然后送给他心仪的女生~于是他用其中一些珠子做成了长度为n ...

  5. Python—— 性能分析入门指南

    虽然并非你编写的每个 Python 程序都要求一个严格的性能分析,但是让人放心的是,当问题发生的时候,Python 生态圈有各种各样的工具可以处理这类问题. 分析程序的性能可以归结为回答四个基本问题: ...

  6. Specification模式的一个不错的示例代码

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Specifi ...

  7. 自制MVC框架原理介绍

    当初用jsp开发程序时,因为很多东西写在一起混淆的,项目做大或者变更的时候就会很吃力,联动性太大,有时修改视图的东西都可能会影响业务逻辑,分层不明确. 后来听说了Struts MVC,做过几个示例,层 ...

  8. python 脚本(获取指定文件夹、指定文件格式、的代码行数、注释行数)

    1.代码的运行结果: 获取 指定文件夹下.指定文件格式 文件的: 总代码行数.总注释行数(需指定注释格式).总空行数: #coding: utf-8 import os, re # 代码所在目录 FI ...

  9. Event-Souring模式

    Event-Sourcing模式使用仅附加存储来记录或描写叙述域中数据所採取的动作,从而记录完整的一系列系列事件,而不是仅存储实体的当前状态.由于存储包括全部的事件,能够用来具体化域对象. Event ...

  10. iOS5 ARC,IBOutlets 应该定义strong还是weak

    转自:http://blog.csdn.net/yiyaaixuexi/article/details/7864974 写这篇文章的缘由,是因为我泡在stackoverflow上翻帖子,看到一个名为S ...