I want to use TBB threads in C++ and want to use "tbb_thread" API.
for example i have static function in class as below
template < typename threadFuncParamT >
class ThreadWrapper
{
public:
static int ThreadRoutineFunction(void* pParam);
};
I want to use tbb_thread API to spawn a thread 开发者_开发知识库with "ThreadRoutineFunction" which is defined above class. How can i achieve this using tbb_thread API. please note that i have to pass pointer to the thread routine function. Can any one give me simple example how to do this?
It sounds like your question is really "how do I get a pointer to a static
member function?"
C++ doesn't officially have a way to do that. However, according to the C++ FAQ (the Note in question 2), "pointers-to-static
-member-functions are usually type-compatible with regular pointers-to-functions."
Your options are:
Use a regular pointer to function, point it at your
static
member function, see if your compiler complains:int (*ptrFunction)(void*) = ThreadWrapper<Foo>::ThreadRoutineFunction;
Do what the FAQ suggests, and declare your function
extern "C"
as well asstatic
(you'll also have to declare the pointer to the function asextern "C"
, and you won't be able to overload the function):template <typename T> class ThreadWrapper { public: extern "C" static int ThreadRoutineFunction(void* param); }; extern "C" int(*ptrCFunction)(void*) = ThreadWrapper<Foo>::ThreadRoutineFunction;
Create an additional function that does nothing but call the
static
member function:template <typename T> int ThreadWrapperHelper(void* param) { return ThreadWrapper<T>::ThreadRoutineFunction(param); } int (*ptrFunction)(void*) = ThreadWrapperHelper<Foo>;
精彩评论