Last active
July 23, 2020 09:05
-
-
Save nasitra/aa378d536888e20056ce to your computer and use it in GitHub Desktop.
Use member function for pthread and detach in C++
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
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