zoukankan      html  css  js  c++  java
  • 造成死锁的各种情况

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_sync(queue, ^{/* a task */});

    After the dispatch_sync function is called, the function doesn’t return until the specified task is finished. It is like a brief version of the dispatch_group_wait function. As you see, the source code is very simple. You can use the dispatch_sync function very easily although it can produce a problem called deadlock. For example, when the following source code is executed on the main thread, it causes a deadlock.

    dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_sync(queue, ^{NSLog(@"Hello?");});

    This source code adds a Block to the main dispatch queue; that is, the Block will be executed on the main thread. At the same time, it waits for the block to be finished. Because it is running on the main thread, the Block in the main dispatch queue is never executed. Let’s see the other example:

    dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_async(queue, ^{

    dispatch_sync(queue, ^{NSLog(@"Hello?");}); });

    The Block that is running on the main dispatch queue is waiting for the other Block to be finished, which will run on the main dispatch queue as well. It causes deadlock. 

  • 相关阅读:
    Web 日志分析过程
    nginx系列之九:lua服务
    Linux网络编程之IO模型
    从URL输入到页面展现到底发生什么
    CentOS 日常运维十大技能
    以MySQL为例,详解数据库索引原理(1)
    Elasticsearch的特点以及应用场景
    Ubuntu1804编译安装LNMP
    golang 高级
    Centos7 安装 Redis
  • 原文地址:https://www.cnblogs.com/studyNT/p/4633971.html
Copyright © 2011-2022 走看看