Skip to content

Instantly share code, notes, and snippets.

@AlexTape
Created June 23, 2015 21:49
Show Gist options
  • Save AlexTape/0a31c1086592e5807c3f to your computer and use it in GitHub Desktop.
Save AlexTape/0a31c1086592e5807c3f to your computer and use it in GitHub Desktop.
class MyThreadClass
{
public:
MyThreadClass() {/* empty */}
virtual ~MyThreadClass() {/* empty */}
/** Returns true if the thread was successfully started, false if there was an error starting the thread */
bool StartInternalThread()
{
return (pthread_create(&_thread, NULL, InternalThreadEntryFunc, this) == 0);
}
/** Will not return until the internal thread has exited. */
void WaitForInternalThreadToExit()
{
(void) pthread_join(_thread, NULL);
}
protected:
/** Implement this method in your subclass with the code you want your thread to run. */
virtual void InternalThreadEntry() = 0;
private:
static void * InternalThreadEntryFunc(void * This) {((MyThreadClass *)This)->InternalThreadEntry(); return NULL;}
pthread_t _thread;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment