1

As the title suggests: What are the differences. I know that QThreadStorage existed long before the thread_local keyword, which is probably why it exists in the first place - but the question remains: Is there any significant difference in what they do between those two (except for the extended API of QThreadStorage).

Im am assuming a normal use case of a static variable - as using QThreadStorage as non static is possible, but not recommended to do.

static thread_local int a;
// vs
static QThreadStorage<int> b;
RAM
  • 2,257
  • 2
  • 19
  • 41
Felix
  • 6,885
  • 1
  • 29
  • 54

1 Answers1

1

Well, since Qt is open-source you can basically figure out the answers from the Qt sources. I am currently looking at 5.9 and here are some things, that I'd classify as significant:

1) Looking at qthreadstorage.cpp, the get/set methods both have this block in the beginning:

QThreadData *data = QThreadData::current();
if (!data) {
    qWarning("QThreadStorage::set: QThreadStorage can only be used with threads started with QThread");
    return 0;
}

So (an maybe this has changed/will change) you can't mix QThreadStorage with anything else than QThread, while the thread_local keyword does not have similar restrictions.

2) Quoting cppreference on thread_local:

thread storage duration. The object is allocated when the thread begins and deallocated when the thread ends.

Looking at qthreadstorage.cpp, the QThreadStorage class actually does not contain any storage for T, the actual storage is allocated upon the first get call:

QVector<void *> &tls = data->tls;
if (tls.size() <= id)
    tls.resize(id + 1);
void **v = &tls[id];

So this means that the lifetime is quite different between these two - with QThreadStorage you actually have more control, since you create the object and set it with the setter, or default initialize with the getter. E.g. if the ctor for the type could throw, with QThreadStorage you could catch that, with thread_local I am not even sure.

I assume these two are significant enough not to go any deeper.

Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71
  • `thread_local` is in effect `static` duration but in the context of a thread. – Mgetz Jun 14 '18 at 15:45
  • @Mgetz - yeah, I guess that is a more simple way to put the the second point. – Rudolfs Bundulis Jun 14 '18 at 15:51
  • Apparently, there is one more difference: on windows, `thread_local` cannot be an exported symbol - QThreadStorage however can be (See https://stackoverflow.com/questions/32034857/can-thread-local-be-used-to-provide-a-static-global-variable-in-a-dll-for-each-t) – Felix Jun 14 '18 at 17:40