<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Thierry,<div class=""><br class=""></div><div class="">Are you initializing the mutex with pthread_mutex_init()? For example:</div><div class=""><br class=""></div><div class="">&nbsp; pthread_mutex_init(&amp;mutex, nil)</div><div class=""><br class=""></div><div class="">Just calling the default initializer for pthread_mutex_t is not sufficient — you won’t get any mutual exclusion. Thread Sanitizer should be complaining about this already (something like “Use of an uninitialized or destroyed mutex”).</div><div class=""><br class=""></div><div class="">If that doesn’t fix the problem, would you mind filing a bug at&nbsp;<a href="http://bugs.swift.org" class="">bugs.swift.org</a>&nbsp;and attaching your test project? That will help us narrow it down and we can continue the investigation there.</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Devin</div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 25, 2017, at 8:23 AM, Thierry Passeron via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Hi everyone,<br class=""><br class="">I don’t know if it’s the good place to ask for this, so if it’s not, please be kind enough to tell me where I should post this question.<br class=""><br class="">I’m having a hard time figuring out why, since I activated ThreadSanitizer to my Xcode 9 scheme, I keep seeing race conditions when using OperationQueue and a custom BlockOperation subclass I made to deal with Asynchronous operations.<br class=""><br class="">If I refer to the documentation on Operation, it says that isCancelled, isFinished, isExecuting properties must be thread safe since you never know from which thread they can be invoked. So I decided to go for a pthread_mutex_lock/unlock to protect the critical parts. For instance isFinished looks like this:<br class=""><br class="">class AsyncBlockOperation : BlockOperation {<br class=""><br class=""> &nbsp;enum State: String {<br class=""> &nbsp;&nbsp;&nbsp;case &nbsp;ready = "Ready",<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;executing = "Executing",<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;finished = "Finished"<br class=""> &nbsp;&nbsp;&nbsp;fileprivate var keyPath: String {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "is" + self.rawValue<br class=""> &nbsp;&nbsp;&nbsp;}<br class=""> &nbsp;}<br class=""><br class=""> &nbsp;private var state: State = .ready<br class=""> &nbsp;{<br class=""> &nbsp;&nbsp;&nbsp;willSet {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;willChangeValue(forKey: state.keyPath)<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;willChangeValue(forKey: newValue.keyPath)<br class=""> &nbsp;&nbsp;&nbsp;}<br class=""> &nbsp;&nbsp;&nbsp;didSet {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;didChangeValue(forKey: oldValue.keyPath)<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;didChangeValue(forKey: state.keyPath)<br class=""> &nbsp;&nbsp;&nbsp;}<br class=""> &nbsp;}<br class=""><br class=""> &nbsp;private var mutex = pthread_mutex_t()<br class=""><br class=""> &nbsp;override var isFinished: Bool {<br class=""> &nbsp;&nbsp;&nbsp;pthread_mutex_lock(&amp;mutex)<br class=""> &nbsp;&nbsp;&nbsp;defer { pthread_mutex_unlock(&amp;mutex) }<br class=""> &nbsp;&nbsp;&nbsp;return state == .finished<br class=""> &nbsp;}<br class=""><br class="">/* Same goes for isCancelled, and isExecuting… */<br class="">}<br class=""><br class="">I thought that the problem of « thread-safe » would be solved with these lock/unlock mutex but when an other thread accesses one of the these properties I get a data race warning by the ThreadSanitizer.<br class=""><br class="">What do I do wrong?<br class="">Is it possible that ThreadSanitizer reports a false positive? How should I debug this kind of issue? <br class=""><br class="">Any help would be much appreciated. I can provide a Xcode test project with a running example of this issue.<br class=""><br class="">Thanks in advance.<br class=""><br class="">Best regards,<br class="">Thierry<br class=""><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></div></blockquote></div><br class=""></div></body></html>