问题描述
我使用 ThreadSanitizer 运行了一个测试代码,它在 xpc_connection_set_event_handler 的块中抛出了一个竞争条件,即使我为连接设置了一个同步队列。
xpc_connection_set_event_handler(conn,^(xpc_object_t event) {
xpc_type_t type = xpc_get_type(event);
if (xpc_get_type(event) == xpc_TYPE_DICTIONARY) {
// race condition here
// WARNING: ThreadSanitizer: data race
// ...
} else {
if (event == xpc_ERROR_CONNECTION_INVALID) {
// Error indicates the peer has closed the connection.
// Tear down any associated data structures.
} else {
// Error indicates that service will terminate soon.
// Flush all buffers,finish all work,etc.
}
xpc_release(peer);
}
});
https://developer.apple.com/documentation/xpc/1448786-xpc_connection_set_target_queue?language=objc: “xpc 运行时保证,当目标队列是串行队列时,事件处理程序块将相对于提交到同一队列的其他块同步执行。当目标队列是并发队列时,事件处理程序块可以并发运行与提交到该队列的其他块,但它永远不会与其他对同一连接的自身调用同时运行"
根据上面的描述看起来应该没有竞争条件,但是为什么我遇到了ThreadSanitizer报告的竞争条件问题?
xpc_connection_set_event_handler 的块是否需要同步以避免竞争条件?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)