views:

129

answers:

2

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.

A: 

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.

Kristopher Johnson
but the problem is that i can't use that kind of thread level constant in template metaprogramming
Gokul
+3  A: 

Templates are compile time constructs, threads are run-time ones - there is no way of having templates specific to a thread.

anon
Thanks, but don't you think they are possible? By your argument, even processes are run-time ones, but we can have process specific constants.
Gokul
@Gokul How? Please post some code that illustrates such a thing. constants must exist and be specified at compile time, otherwise they are not constants.
anon
@Gokul: You *need* to give an example of what you're trying to do. C++ has no concept of "process specific constants".
GMan
@Neil: At compile time, i know there are a two pools of threads and i know one pool will always have a constant value and other pool will always have a different value. OK you are saying that even though they are constants for the thread duration, i can't mention it at compile time. Thanks.
Gokul
@Gokul You may know that, but the c++ compiler doesn't. The only way to do that is to give the constants different compile-time names.
anon
@GMan! i might have used the wrong terminology, but it is possible to create two executables by changing a static const variable. One process can invoke the next one. All i am trying to say is that the code being maintained is less, even though i have two executables, but there is no such facility with threads
Gokul
@Gokul You really, really need to post some actual C++ code that illustrates what you are talking about.
anon