You can create your own private stream storage as a single point for each stream identifier. Something like that:
struct ThreadLocalStorage { ThreadLocalStorage() { // initialization here } int my_static_variable_1; // more variables }; class StorageManager { std::map<int, ThreadLocalStorage *> m_storages; ~StorageManager() { // storage cleanup std::map<int, ThreadLocalStorage *>::iterator it; for(it = m_storages.begin(); it != m_storages.end(); ++it) delete it->second; } ThreadLocalStorage * getStorage() { int thread_id = GetThreadId(); if(m_storages.find(thread_id) == m_storages.end()) { m_storages[thread_id] = new ThreadLocalStorage; } return m_storages[thread_id]; } public: static ThreadLocalStorage * threadLocalStorage() { static StorageManager instance; return instance.getStorage(); } };
GetThreadId (); It is a platform-specific function for determining the caller ID. Something like that:
int GetThreadId() { int id; #ifdef linux id = (int)gettid(); #else
Now, as part of the stream function, you can use local storage:
void threadFunction(void*) { StorageManager::threadLocalStorage()->my_static_variable_1 = 5;
GreenScape Sep 30 '11 at 7:19 2011-09-30 07:19
source share