3

I need to use a QSharedPointer for the reference counting, but I'll use it in a single-thread application so I don't want the thread-safety overhead (which I assume is fairly expensive).

Is there a way to turn off the thread-safety, or is there an equivalent Qt class that offers reference-counting only? If not, are there stand-alone classes that don't require me to include a big library like boost, or turn on c++11?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • Possibly [`QSharedDataPointer`](http://doc.qt.io/qt-5/qshareddatapointer.html) – NathanOliver Jan 20 '16 at 15:27
  • @NathanOliver Unfortunately, not. From the docs: "QSharedDataPointer implements thread-safe reference counting" – sashoalm Jan 20 '16 at 15:31
  • I don't think you will be able to get away from that. Even boost and C++11 do that. At least the access to the underlying pointer is not thread safe so that takes care of most of the overhead. – NathanOliver Jan 20 '16 at 15:32
  • 6
    It's not that expensive. As far as system operation and/or CPU commands sure, they take a lot longer than not doing that...but compared to the rest of the application's activity is generally very rare that it'll show up on a profile as significant in any way whatsoever. – Edward Strange Jan 20 '16 at 15:54
  • 1
    I'd like to add that it's not just inexpensive, it's inexpensive because it doesn't do any locking. It's just about atomic increment/decrement instructions which are really fast. Slower than non-atomic, of course, but not that much. It it used mutexes, then it would probably be expensive, but it doesn't. And if you have some ridiculously complicated algorithm that uses that pointer a lot, you can always query the internal pointer, save it in a local variable and use that in your crazy nested loops. – Sergei Tachenov Jan 20 '16 at 16:21
  • You offer no substance to your assertion that there *is* an overhead to begin with. You cannot just muse about such things, you absolutely must measure the overhead and prove to yourself that you're not imagining things. Or at least you should point out to Qt code that introduces the overhead, with some understanding/explanation of why you expect the overhead to be important in your application. Side note: "Turning on" C++11 is the logical thing to do - you're making your code look much worse for not using it. – Kuba hasn't forgotten Monica Jan 21 '16 at 14:57
  • @KubaOber I thought there is a non-zero overhead. I don't know how big, though. Do you mean there is no overhead, or just that it's a small overhead? – sashoalm Jan 22 '16 at 14:43
  • @sashoalm There is no way to answer it. Modern CPUs are clever enough that there may be no overhead at all. Measure it. Compare to your own code. My bet is that any time you spend thinking about it will be much greater than the overhead in your application over 24hr of runtime. – Kuba hasn't forgotten Monica Jan 22 '16 at 15:11
  • @KubaOber Fair enough. I just assumed that the Qt guys themselves wouldn't have created QScopedPointer if they didn't think QSharedPointer has some overhead. I mean, you can just use QSharedPointer instead of QScopedPointer otherwise. But maybe it's the reference counting they wanted to skip and not the thread-safety, especially when you need a heap allocation for it (I think the reference counter is heap-allocated). Here's a performance test I found - http://justinfx.com/2015/06/07/comparing-performance-of-qt-smart-pointer-options/. It seems there is indeed an overhead. – sashoalm Jan 22 '16 at 16:28
  • `QScopedPointer` guarantees unique ownership. You can get some nice memory leaks if you use `QSharedPointer` indiscriminately in place of `QScopedPointer`: it won't stop you from certain ways of shooting yourself in the foot. The reason for existence of these two pointer types isn't overhead but clarity of intent and preventing errors. – Kuba hasn't forgotten Monica Jan 22 '16 at 17:16

1 Answers1

0

Qt 5 has very clever realization of class QMutex. And it must not to use Synchronization Primitives of OS in a single-thread applications. So, I think that using of Qt's smart pointers is a good solution.

Vladimir Bershov
  • 2,701
  • 2
  • 21
  • 51