zoukankan      html  css  js  c++  java
  • GCD中的dispatch_barrier_async函数的使用(栅栏函数)

    <一>什么是dispatch_barrier_async函数

    毫无疑问,dispatch_barrier_async函数的作用与barrier的意思相同,在进程管理中起到一个栅栏的作用,它等待所有位于barrier函数之前的操作执行完毕后执行,并且在barrier函数执行之后,barrier函数之后的操作才会得到执行,该函数需要同dispatch_queue_create函数生成的concurrent Dispatch Queue队列一起使用

    <二>dispatch_barrier_async函数的作用

    1.实现高效率的数据库访问和文件访问

    2.避免数据竞争

    <三>dispatch_barrier_async实例

    - (void)barrier
    {
      //同dispatch_queue_create函数生成的concurrent Dispatch Queue队列一起使用 dispatch_queue_t queue
    = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ NSLog(@"----1-----%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"----2-----%@", [NSThread currentThread]); }); dispatch_barrier_async(queue, ^{ NSLog(@"----barrier-----%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"----3-----%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"----4-----%@", [NSThread currentThread]); }); }

    输出结果:1 2 --> barrier -->3 4  其中12 与 34 由于并行处理先后顺序不定

  • 相关阅读:
    管理~资源组织运作
    科学与艺术区别
    概念思维
    拨开文字表象,关注背后事实
    论信息部门与业务部门的关系
    再论信息系统
    linux命令行快捷键记录
    hadoop,帮我解了部分惑的文章
    hadoop运行测试命令遇到的问题
    日志分析及几个小技巧
  • 原文地址:https://www.cnblogs.com/denz/p/5277666.html
Copyright © 2011-2022 走看看