Android的锁是对Linux锁的一种包装:

// ---------------------------------------------------------------------------
namespace android {
// --------------------------------------------------------------------------- class Condition; /*
* Simple mutex class. The implementation is system-dependent.
*
* The mutex must be unlocked by the thread that locked it. They are not
* recursive, i.e. the same thread can't lock it multiple times.
*/
class Mutex {
public:
enum {
PRIVATE = 0,//该锁为本进程内使用
SHARED = 1//该锁跨进程使用
}; Mutex();
Mutex(const char* name);
Mutex(int type, const char* name = NULL);
~Mutex(); // lock or unlock the mutex
status_t lock();
void unlock(); // lock if possible; returns 0 on success, error otherwise
status_t tryLock(); // Manages the mutex automatically. It'll be locked when Autolock is
// constructed and released when Autolock goes out of scope.
class Autolock {//内部类,用来管理mutex的--相当于“智能指针”
public:
inline Autolock(Mutex& mutex) : mLock(mutex) { mLock.lock(); }
inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); }
inline ~Autolock() { mLock.unlock(); }
private:
Mutex& mLock;//持有mutex的引用
}; private:
friend class Condition;//友元类,Condition是在mutex的基础上使用的 // A mutex cannot be copied
Mutex(const Mutex&);//设为私有,无法调用“拷贝构造函数”
Mutex& operator = (const Mutex&); #if defined(HAVE_PTHREADS)
pthread_mutex_t mMutex;//使用linux的pthread_mutex_t这个互斥锁
#else
void _init();
void* mState;
#endif
}; // --------------------------------------------------------------------------- #if defined(HAVE_PTHREADS) inline Mutex::Mutex() {
pthread_mutex_init(&mMutex, NULL);
}
inline Mutex::Mutex(const char* name) {
pthread_mutex_init(&mMutex, NULL);
}
inline Mutex::Mutex(int type, const char* name) {
if (type == SHARED) {//if分支:根据传进来的值,决定是否构造“跨进程”的锁
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&mMutex, &attr);
pthread_mutexattr_destroy(&attr);
} else {
pthread_mutex_init(&mMutex, NULL);//if分支:本进程内的锁
}
}
inline Mutex::~Mutex() {
pthread_mutex_destroy(&mMutex);
}
inline status_t Mutex::lock() {
return -pthread_mutex_lock(&mMutex);
}
inline void Mutex::unlock() {
pthread_mutex_unlock(&mMutex);
}
inline status_t Mutex::tryLock() {
return -pthread_mutex_trylock(&mMutex);
} #endif // HAVE_PTHREADS // --------------------------------------------------------------------------- /*
* Automatic mutex. Declare one of these at the top of a function.
* When the function returns, it will go out of scope, and release the
* mutex.
*/ typedef Mutex::Autolock AutoMutex; // ---------------------------------------------------------------------------
}; // namespace android
  

  

android分析之mutex的更多相关文章

  1. Android 分析工具 APKAnalyser

    APKAnalyser 是 Android 静态,虚拟分析工具,用来测试和验证 Android 应用的开发工作.ApkAnalyser 是个完整的工具链,可以修改二进制应用.用户可以改装,安装,运行, ...

  2. cocos android分析

    来自:http://xiebaochun.github.io/ cocos2d-x Android环境搭建 cocos2d-x环境搭建比較简单,可是小问题还是不少,我尽量都涵盖的全面一些. 下载软件  ...

  3. Android分析应用程序的构建过程

    为了方便Android应用开发要求我们Androidproject编制和包装有了更深入的了解,例如,我们知道这是做什么的每一步,境和工具.输入和输出是什么.等等. 在前文<命令行下Android ...

  4. android分析windowManager、window、viewGroup之间关系(二)

    三.接上一节,分析windowManager中添加一个悬浮框的方式,首先看代码 WindowManager.LayoutParams params = new LayoutParams(); para ...

  5. android分析windowManager、window、viewGroup之间关系(一)

    本文将主要介绍addview方法,在windowManager.window.viewGroup中的实现原理.首先将介绍这些类结构关系,然后分析其内在联系,介绍实现原理,最后介绍重要的一个参数wind ...

  6. [Android] 分析一个CalledFromWrongThreadException崩溃

    1 问题描述 问题本身比较清晰简单,但推敲的过程中发现了不少有意思的东西. 在C++ SDK回调JNI至Java Observer函数中,直接操作了UI界面textView.setText(msg), ...

  7. android分析之Binder 01

    终于还是得写一篇关于Binder的文章了.从最初接触Android到花大把时间研究Android源码,Binder一直是分析道路的拦路虎.看了几本最流行的Android源码分析书籍,每次基本上都不能把 ...

  8. android分析之消息处理

    前序:每个APP对应一个进程,该进程内有一个ActivityThread的线程,称为主线程(即UI主线程),此外,还有其他线程,这个再论. android的消息系统分析. 每个Thread只对应一个L ...

  9. android分析之Condition

    Condition的含义是条件变量,其实现依赖于系统,一般都要配合Mutex使用,使用步骤为:给mutex上锁(Lock),调用wait等待"条件"发生,如果没有发生则re-wai ...

随机推荐

  1. LEETCODE - 1228【等差数列中缺失的数字】

    C++: class Solution { public:     int missingNumber(vector<int>& arr) {         int subnum ...

  2. 计组CPU设计实验关键材料和关键设计

    我记得这是2016春季学期搞得,参考和学习了很多别人的东西,这里小小的总结一下,逻辑性还不是太强,还需要好好整理 首先是指令集 CPU架构 外部接线架构 指令格式 机器状态自动机 这部分忘了,汗 这部 ...

  3. 重学c#————struct

    前言 简单整理一下struct. 正文 struct 对于struct 而言呢,我们往往会拿class作为对比,但是呢,我们在初学阶段用class来替代struct,struct的存在感越来越低了. ...

  4. leetcode best solutions

    leetcode best solutions how to learning algorithms form the leetcode best solutions https://leetcode ...

  5. Web Components & HTML5 & template & slot

    Web Components & HTML5 & template & slot https://developer.mozilla.org/en-US/docs/Web/HT ...

  6. Objec.assign & bug

    Objec.assign & bug shallow copy https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Referenc ...

  7. 倒计时2天!2021 NGK新加坡区块链峰会与你不见不散!

    1月31日14时,NGK将于新加坡召开"2021 NGK新加坡区块链峰会",并将在全球开启同步直播.   据了解,本次峰会将汇聚全球二十多位顶尖区块链专家学者,与NGK灵石技术团队 ...

  8. 解决ROS及Fast-RTPS安装和使用中raw.githubusercontent.com无法连接的问题

    资料参考: https://blog.csdn.net/weixin_44692299/article/details/105869229

  9. MySQL 导入外部数据

    手工为数据库录入数据: 1 -- 使用数据库 2 use test; 3 4 -- 创建fruits数据表 5 create table fruits( 6 f_id char(10) not nul ...

  10. 安装mysql报错

    原文链接:https://blog.csdn.net/bao19901210/article/details/51917641 二进制安装 1.添加mysql组和mysql用户,用于设置mysql安装 ...