我有以下的QThreadStorage問題和提升的thread_specific_ptr:限制對Qt和升壓線程本地存儲
1)是否有可存儲在Qthreadstorage對象的數量有何限制?我遇到了一個關於256個QThreadStorage對象的qt查詢,所以想澄清這個限制指向什麼?
2)QThreadStorage是否僅適用於QThreads?
3)boost tls有沒有限制?
4)我有一個用例,我想在tls上進行操作,並在所有線程完成進一步處理時將數據同步到主線程。我寫了下面的代碼,並且想檢查下面的代碼是否可以。
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/thread/tss.hpp>
boost::mutex mutex1;
int glob = 0;
class data
{
public:
char* p;
data()
{
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
std::cout << "here" << std::endl;
}
int main(int argc, char* argv[])
{
boost::thread_group threads;
for (int i=0; i<10; ++i)
threads.create_thread(&thread_proc);
threads.join_all();
for (int i=0; i<10; ++i)
puts(global_p[i]);
}