Skip to content

Instantly share code, notes, and snippets.

@ckaminski
Created August 10, 2012 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckaminski/3318511 to your computer and use it in GitHub Desktop.
Save ckaminski/3318511 to your computer and use it in GitHub Desktop.
C++ invoker
Heres a rough idea, how you can use this with any function object:
template<class F>
struct invoker
{
static void start(void * x)
{
(*(reinterpret_cast<F*>(x)))();
delete x;
}
};
template<class F>
int create(pthread_t *thread, const pthread_attr_t *attr, F f)
{
return pthread_create(thread, attr, &invoker<F>::start, (void *) new F(f));
}
It allocates the function object on to the heap which may not be the best option.
http://www.reddit.com/r/cpp/comments/xgnqi/c_reflection_in_under_100_lines_of_code/c5mnaga
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment