Is there a way by which we can simulate thread level constants in C++? For example, if i have to make a call to template functions, then i need to mention the constants as template level parameters? I can use static const variables for template metaprogramming, but they are process level constants.
I know, i am asking a question with a high probability of 'No'. Just thought of asking this to capitalize on the very rare probability :))
开发者_如何转开发On request, i am posting a sample code. Here i needed to track the enquiry, if it comes from one specific thread. I assume that, if i create that as my first thread, then it will get the thread id 1.
template<ACE_INT32 ThreadId>
bool enquire_presence( Manager* man)
{
return check(man);
}
template<>
bool enquire_presence<1>( Manager* man )
{
track_enquiry(man);
return check(man);
}
Thanks, Gokul.
Templates are compile time constructs, threads are run-time ones - there is no way of having templates specific to a thread.
Check out Boost's Thread Local Storage.
However, I'm not sure this will give you the template metaprogramming capability you want. You may have to explicitly define a constant value for each thread you expect to create.
精彩评论