<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=""> pthread_mutex_init(&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 <a href="http://bugs.swift.org" class="">bugs.swift.org</a> 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 <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> 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=""> enum State: String {<br class=""> case ready = "Ready",<br class=""> executing = "Executing",<br class=""> finished = "Finished"<br class=""> fileprivate var keyPath: String {<br class=""> return "is" + self.rawValue<br class=""> }<br class=""> }<br class=""><br class=""> private var state: State = .ready<br class=""> {<br class=""> willSet {<br class=""> willChangeValue(forKey: state.keyPath)<br class=""> willChangeValue(forKey: newValue.keyPath)<br class=""> }<br class=""> didSet {<br class=""> didChangeValue(forKey: oldValue.keyPath)<br class=""> didChangeValue(forKey: state.keyPath)<br class=""> }<br class=""> }<br class=""><br class=""> private var mutex = pthread_mutex_t()<br class=""><br class=""> override var isFinished: Bool {<br class=""> pthread_mutex_lock(&mutex)<br class=""> defer { pthread_mutex_unlock(&mutex) }<br class=""> return state == .finished<br class=""> }<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>