zthread学习 实例六 访问控制

来源:互联网 发布:windows.old删除之后 编辑:程序博客网 时间:2024/05/13 03:38

就是对共享资源的同步访问,以免造成不确定的状态。

Zhread库是基于Mutex临界区来组建同步机制的。


Guard<>模板很方便的定义了同步机制,它在创建时用acquire()来获得一个Lockable对象,被销毁时用release()来释放这个锁。Guard对象的创建很好的利用了变量作用域概念。例如:

 

void fun()

{

      ... ...

      {    //特意添加的用于控制Guard作用域的括号

            Guard<Mutex> g(Lock);

            ... ...//重点临界区代码

       }

}

 

Zhread库中给Guard<>提供了4种类型的锁策略:

1、CompoundScope

     Note: Locking policy that aggregates two policies that share a target.It is not appropriate to use with any type of OverlappedScope

 

2、LockedScope(默认)

     Note: Locking policy for Lockable objects. This policy acquire()s a Lockable when the protection scope is created, and it release()s a Lockable when the scope is destroyed.

 

3、UnlockedScope(解锁)

     Note: Locking policy for Lockable objects. This policy release()s a Lockable when the protection scope is created, and it acquire()s a Lockable when the scope is destroyed.

4、TimedLockedScope(带时间)

      Note:  Locking policy that attempts to enterScope some resource in a certain amount of time using an tryEnterScope-relase protocol.

 

5、OverlappedScope

    Note:  Locking policy allows the effective scope of two locks to overlap by releasing and disabling one lock before its Guard does so.

原创粉丝点击