Hi Thiago, On Sunday February 5 2012, Thiago Macieira wrote: > However, in practice, many people have begun relying on an implementation > detail that read-only operations (the const methods) in those classes are > thread-safe. That is, a shared object can be accessed without mutex > protection provided only const functions are called. > > That is not codified in the documentation. > > I'd like to propose that it is and we say that all const methods in the > implicitly-shared classes are thread-safe. It should also explain that > calling a non-thread-safe method in the same object simultaneously with a > thread-safe one breaks the safety (which is different to what > "thread-safety" means to QCoreApplication::postEvent).
The correct way for formulate, I think, would be that objects originally declared const are thread-safe. That is, const QString s = ...; makes accessing s thread-safe (read QString as a placeholder for any implicitly shared class). In addition, const_casting &s to non-const QString now results in undefined behaviour. There are two caveats, though: construction needs to have finished. And the question of when the result of the construction is actually seen by other threads might pose a problem, too. iow: In the C++11 memory model, unless the s.d pointer is atomic, I believe this is still racy. However, once you've established that construction of 's' has C++11-happened-before any other use of 's' (via means external to QString, e.g. by using Q_GLOBAL_STATIC[1]), I agree with you that the implementation should guarantee thread-safety. Thanks, Marc [1] new QString(args) is sequenced-before assignment to atomic pointer to argument, which inter-thread-happens-before any use (hopefully, by design of Q_GLOBAL_STATIC), which is sequenced before return of the function created in Q_GLOBAL_STATIC, which is sequenced before any use of the result value of the function. -- Marc Mutz <[email protected]> | Senior Software Engineer KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company www.kdab.com || Germany +49-30-521325470 || Sweden (HQ) +46-563-540090 KDAB - Qt Experts - Platform-Independent Software Solutions _______________________________________________ Development mailing list [email protected] http://lists.qt-project.org/mailman/listinfo/development
