开发者

spawning thread using static function using TBB in C++

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:

  1. Use a regular pointer to function, point it at your static member function, see if your compiler complains:

    int (*ptrFunction)(void*) = ThreadWrapper<Foo>::ThreadRoutineFunction;
    
  2. Do what the FAQ suggests, and declare your function extern "C" as well as static (you'll also have to declare the pointer to the function as extern "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;
    
  3. 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>;
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜