Take the 2-minute tour ×

I have following questions on QThreadStorage and boost's thread_specific_ptr:

1) Is there any limitation on number of objects that can be stored in Qthreadstorage? I came across a qt query about 256 QThreadStorage objects, so like to clarify what this limitation points to?

2) Does QThreadStorage work only with QThreads?

3) Is there any limitation on boost tls?

4) I have a use case where I want to operate on tls and sync the data to main thread when all threads finish for further processing. I wrote the below code and like to check if the below code is okay.

#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/thread/tss.hpp>

boost::mutex mutex1;
int glob = 0;

class data
    char* p;
            p = (char*)malloc(10);
        sprintf(p, "test%d\n", ++glob);

char* global_p[11] = {0}; 
int index = -1;

void cleanup(data* _ignored) {
    std::cout << "TLS cleanup" << std::endl;
boost::mutex::scoped_lock lock(mutex1);
global_p[++index] = _ignored->p;

boost::thread_specific_ptr<data> value(cleanup);

void thread_proc()
    value.reset(new data()); // initialize the thread's storage

int main(int argc, char* argv[])
    boost::thread_group threads;

share|improve this question

1 Answer 1

I can partially answer your question.

  1. The 256 limit belongs to old qt. Probably you are reading old documentation. New qt version (i.e above 4.6) does not have such limit

  2. QThreadStorage can destroy contained items at thread exit because it works closely with QThread. So separting these two is not a wise idea in my opinion.

  3. Here I think you are asking the number of objects that can be stored with boost tls. I am not aware of any limitation on boost tls. You should be fine.

  4. Your code looks OK to me except in the constructor of data you need to put a mutex lock before ++glob otherwise you may not get an incrementing value.

I hope this helps.

share|improve this answer

Your Answer


