What happens when we call clear on a variable of type std::list
which is also marked thread_local
? Does it give up the allocated memory?
In my scenario I have a thread_local
list which I populate in one iteration and clear by the end of the iteration. Is the thread_local
not very useful in such a case as we give up the memory by the end of the iteration (and have to allocate memory again in the next iteration)?
The marking of a variable as thread_local
has no effect - AFAIK - on its behavior when allocating memory. So you should get the same behavior w.r.t. de-allocation on clear()
with a non-qualified list, a thread-local list, a static list etc.
Also, note that std::list
with the standard allocator allocates space for its nodes on the heap - the single, process-wide heap (ses also the paragraph quoted from the standard in this question about this point), and there's nothing thread-local about those allocations themselves. It's just the who-list-level data members (e.g. the pointer to the first node) that's thread-local.
PS - I'm not sure the standard actually mandates that any memory be freed on calling clear()
- but it definitely allows for it, in that the time complexity of this method can be linear.