swift OSAtomicIncrement32Barrier已弃用,如何解决此问题?

az31mfrm  于 2022-10-31  发布在  Swift
关注(0)|答案(1)|浏览(193)

我尝试在我的ios应用程序上使用Xcode14和ios 16解析服务器,我安装了pod解析,但当我运行代码时,我得到以下警告消息:

'OSAtomicIncrement32Barrier' is deprecated: first deprecated in iOS 10.0 - Use atomic_fetch_add() from <stdatomic.h> instead

任何有关如何解决此问题的帮助:

+ (instancetype)taskForCompletionOfAllTasks:(nullable NSArray<BFTask *> *)tasks {
    __block int32_t total = (int32_t)tasks.count;
    if (total == 0) {
        return [self taskWithResult:nil];
    }

    __block int32_t cancelled = 0;
    NSObject *lock = [[NSObject alloc] init];
    NSMutableArray *errors = [NSMutableArray array];

    BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];
    for (BFTask *task in tasks) {
        [task continueWithBlock:^id(BFTask *t) {
            if (t.error) {
                @synchronized (lock) {
                    [errors addObject:t.error];
                }
            } else if (t.cancelled) {
                OSAtomicIncrement32Barrier(&cancelled); // error is here
            }

            if (OSAtomicDecrement32Barrier(&total) == 0) { // error is here
                if (errors.count > 0) {
                    if (errors.count == 1) {
                        tcs.error = [errors firstObject];
                    } else {
                        NSError *error = [NSError errorWithDomain:BFTaskErrorDomain
                                                             code:kBFMultipleErrorsError
                                                         userInfo:@{ BFTaskMultipleErrorsUserInfoKey: errors }];
                        tcs.error = error;
                    }
                } else if (cancelled > 0) {
                    [tcs cancel];
                } else {
                    tcs.result = nil;
                }
            }
            return nil;
        }];
    }
    return tcs.task;
}

xpcnnkqh

xpcnnkqh1#

C11具有support for atomics,因此您可以执行以下操作:


# include <stdatomic.h>

// Declare an atomic int and initialize it to zero
atomic_int total;
atomic_init(&total, 0);

// Note: C17 and C23 allow safe direct initialization
// i.e. atomic_int total = 0;

// Operations such as ++total and --total are atomic
++total;

// Or alternatively
atomic_fetch_add(&total, 1);
atomic_fetch_sub(&total, 1);

相关问题