zoukankan      html  css  js  c++  java
  • @synchronized 再考察

    核心是:将标示对象与锁建立关联。

    线程

    标识;

    异常;

    NSString *test = @"test";

    @try {
        // Allocates a lock for test and locks it
        objc_sync_enter(test);
        test = nil;
    } @finally {
        // Passed `nil`, so the lock allocated in `objc_sync_enter`
        // above is never unlocked or deallocated
        objc_sync_exit(test);   
    }
     

    int objc_sync_enter(id obj)

    {

        int result = OBJC_SYNC_SUCCESS;

        if (obj) {

            SyncData* data = id2data(obj, ACQUIRE);

            require_action_string(data != NULL, done, result = OBJC_SYNC_NOT_INITIALIZED, "id2data failed");

            result = recursive_mutex_lock(&data->mutex);

            require_noerr_string(result, done, "mutex_lock failed");

        } else {

            // @synchronized(nil) does nothing

            if (DebugNilSync) {

                _objc_inform("NIL SYNC DEBUG: @synchronized(nil); set a breakpoint on objc_sync_nil to debug");

            }

            objc_sync_nil();

        }

    done: 

        return result;

    }

    // End synchronizing on 'obj'. 

    // Returns OBJC_SYNC_SUCCESS or OBJC_SYNC_NOT_OWNING_THREAD_ERROR

    int objc_sync_exit(id obj)

    {

        int result = OBJC_SYNC_SUCCESS;

        

        if (obj) {

            SyncData* data = id2data(obj, RELEASE); 

            require_action_string(data != NULL, done, result = OBJC_SYNC_NOT_OWNING_THREAD_ERROR, "id2data failed");

            

            result = recursive_mutex_unlock(&data->mutex);

            require_noerr_string(result, done, "mutex_unlock failed");

        } else {

            // @synchronized(nil) does nothing

        }

  • 相关阅读:
    控件右键菜单的实现以及选中后勾选
    DataGridView控件使用
    return,continue,break的区别
    break和continue的区别 循环终止办法
    事件
    跨线程改变控件属性 线程调用带参数方法
    XML配置文件相关
    抽象类及与接口的区别
    字典
    Oracle学习第一天
  • 原文地址:https://www.cnblogs.com/feng9exe/p/8567783.html
Copyright © 2011-2022 走看看