提问人:Damiano Miazzi 提问时间:10/26/2022 更新时间:10/26/2022 访问量:246
OSAtomicIncrement32Barrier 已弃用,如何解决这个问题?
OSAtomicIncrement32Barrier deprecated, how to solve this issue?
问:
我正在使用 Xcode14 和 ios 16 在我的 ios 应用程序上尝试 Parse 服务器,我安装了 pod Parse,但是当我运行代码时,我收到以下警告消息:
'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;
}
答:
1赞
sbooth
10/26/2022
#1
C11 支持原子,因此您可以执行以下操作:
#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);
评论