Skip to content

Instantly share code, notes, and snippets.

@aleksihakli
Created April 13, 2014 20:33
Show Gist options
  • Save aleksihakli/10601143 to your computer and use it in GitHub Desktop.
Save aleksihakli/10601143 to your computer and use it in GitHub Desktop.
pthread_create cpp wrapper
/**
* @brief create_fun creates pthreads from void* (*)(P*) typed functions.
*
* @param f function as void* (*)(P*) function pointer.
* @param p parameter as P pointer.
*
* @return possible error in thread creation, integer from pthread_create.
*/
template <typename F, typename P>
int create_fun(pthread_t* thread, const pthread_attr_t* attr, F* f, P* p) {
void* (*fun)(void*) = reinterpret_cast<void* (*)(void*)>(f);
void* param = static_cast<void*>(p);
return pthread_create(thread, attr, fun, param);
}
template <typename F, typename P>
int create_fun(pthread_t* thread, F* f, P* p) {
return create_fun(thread, nullptr, f, p);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment