Condition的含义是条件变量,其实现依赖于系统,一般都要配合Mutex使用,使用步骤为:给mutex上锁(Lock),调用wait等待“条件”发生,如果没有发生则re-wait(),最后释放mutex(unlock),并继续执行。所有等待(wait)同一个“条件变量(condition)”的线程都要使用相同的一把锁——这样相当于互斥操作该Condition。

// ---------------------------------------------------------------------------
namespace android {
// --------------------------------------------------------------------------- /*
* Condition variable class. The implementation is system-dependent.
*
* Condition variables are paired up with mutexes. Lock the mutex,
* call wait(), then either re-wait() if things aren't quite what you want,
* or unlock the mutex and continue. All threads calling wait() must
* use the same mutex for a given Condition.
*/
class Condition {
public:
enum {
PRIVATE = 0,//指定释放跨进程共享
SHARED = 1
}; Condition();
Condition(int type);
~Condition();
// Wait on the condition variable. Lock the mutex before calling.
status_t wait(Mutex& mutex);//通过Mutex来对“条件变量”实现互斥访问——多线程、多进程场景
// same with relative timeout
status_t waitRelative(Mutex& mutex, nsecs_t reltime);
// Signal the condition variable, allowing one thread to continue.
void signal();
// Signal the condition variable, allowing all threads to continue.
void broadcast(); private:
#if defined(HAVE_PTHREADS)
pthread_cond_t mCond;//Linux的pthread_cond_t类型
#else
void* mState;
#endif
}; // --------------------------------------------------------------------------- #if defined(HAVE_PTHREADS) inline Condition::Condition() {
pthread_cond_init(&mCond, NULL);
}
inline Condition::Condition(int type) {
if (type == SHARED) {
pthread_condattr_t attr;
pthread_condattr_init(&attr);
pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_cond_init(&mCond, &attr);
pthread_condattr_destroy(&attr);
} else {
pthread_cond_init(&mCond, NULL);
}
}
inline Condition::~Condition() {
pthread_cond_destroy(&mCond);
}
inline status_t Condition::wait(Mutex& mutex) {//系统调用wait包含:释放锁、进入休眠等待、唤醒后重新获取锁
return -pthread_cond_wait(&mCond, &mutex.mMutex);
}
inline status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) {//具有时间控制
#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE)
struct timespec ts;
ts.tv_sec = reltime/1000000000;
ts.tv_nsec = reltime%1000000000;
return -pthread_cond_timedwait_relative_np(&mCond, &mutex.mMutex, &ts);
#else // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
struct timespec ts;
#if defined(HAVE_POSIX_CLOCKS)
clock_gettime(CLOCK_REALTIME, &ts);
#else // HAVE_POSIX_CLOCKS
// we don't support the clocks here.
struct timeval t;
gettimeofday(&t, NULL);
ts.tv_sec = t.tv_sec;
ts.tv_nsec= t.tv_usec*1000;
#endif // HAVE_POSIX_CLOCKS
ts.tv_sec += reltime/1000000000;
ts.tv_nsec+= reltime%1000000000;
if (ts.tv_nsec >= 1000000000) {
ts.tv_nsec -= 1000000000;
ts.tv_sec += 1;
}
return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts);
#endif // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
}
inline void Condition::signal() {
pthread_cond_signal(&mCond);
}
inline void Condition::broadcast() {
pthread_cond_broadcast(&mCond);
} #endif // HAVE_PTHREADS // ---------------------------------------------------------------------------
}; // namespace android

  Barrier是利用上面Condition的一个例子:

class Barrier
{
public:
inline Barrier() : state(CLOSED) { }//state就是所谓的“条件”
inline ~Barrier() { }
void open() {
Mutex::Autolock _l(lock);
state = OPENED;
cv.broadcast();
}
void close() {
Mutex::Autolock _l(lock);
state = CLOSED;
}
void wait() const {
Mutex::Autolock _l(lock);//临时对象_l,用lock来构造,在AutoLock的构造函数里已给lock加锁(调用lock()函数)——该wait()函数执行完毕,会自动释放lock(这个场景会使得其他线程再次修改state,产生不安全因素。不过由于Barrier的使用场景的特殊性,其用在线程初始化时,故OK。)
while (state == CLOSED) {//while语句:不断轮询,直到state==OPENED
cv.wait(lock);
}
}
private:
enum { OPENED, CLOSED };
mutable Mutex lock;//持有一个互斥锁
mutable Condition cv;//持有一个条件变量
volatile int state;//每次都从内存更新的“条件”
};

  

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

  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分析之mutex

    Android的锁是对Linux锁的一种包装: // ------------------------------------------------------------------------- ...

随机推荐

  1. 鸟哥的linux私房菜——第十三章学习(Linux 帐号管理与 ACLL 权限设置)

    第十三章.Linux 帐号管理与 ACLL 权限设置 1.0).使用者识别码: UID 与 GID UID :User ID GID :group ID [root@study ~]# ll -d / ...

  2. leetcode 1 两数之和 hashmap

    主要是hashmap.还有边插入边查找,提高效率和降低空间复杂度. 之前一直用map,结果发现还有hashmap,效率更高. 注意名称空间为 using namespace __gnu_cxx; 问题 ...

  3. TypeScript tuple 元组

    TypeScript tuple 元组 元组类型允许您用固定数量的元素表示数组,这些元素的类型是已知的,但不必相同. "use strict"; /** * * @author x ...

  4. css & background & svg

    css & background & svg https://developer.mozilla.org/en-US/docs/Web/CSS/background backgroun ...

  5. TypeScript 3.7 RC & Assertion Functions

    TypeScript 3.7 RC & Assertion Functions assertion functions, assert https://devblogs.microsoft.c ...

  6. js & class & init

    js & class & init how to call class init method in js when create an instance 在初始化类实例的时候调用,类 ...

  7. js operate svg

    js operate svg js dynamic create svg https://stackoverflow.com/questions/20539196/creating-svg-eleme ...

  8. qt 向窗口发送消息,键盘输入事件

    #include <windows.h> #include <QtDebug> #include <locale> #include <tchar.h> ...

  9. 「NGK每日快讯」2021.2.8日NGK公链第97期官方快讯!

  10. c# 打印面单

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { ...