Skip to content

Instantly share code, notes, and snippets.

@nasitra
Last active July 23, 2020 09:05
Show Gist options
  • Save nasitra/aa378d536888e20056ce to your computer and use it in GitHub Desktop.
Save nasitra/aa378d536888e20056ce to your computer and use it in GitHub Desktop.
Use member function for pthread and detach in C++
int T::method() {
return 0;
}
// static member function
void *T::threadStart(void *obj) {
int ret;
T *me = reinterpret_cast<T *>(obj);
ret = me->method();
return (void *)ret;
}
int T::run(void) {
pthread_t t;
int ret;
ret = pthread_create(&t, NULL, T::threadStart, this);
if (ret) {
printf("Cannot create thread\n");
return ret;
}
ret = pthread_detach(t);
if (ret) {
printf("Cannot detach thread\n");
return ret;
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment