Created
August 10, 2012 22:04
-
-
Save ckaminski/3318511 to your computer and use it in GitHub Desktop.
C++ invoker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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