pthread function from a class
My favorite way to handle a thread is to encapsulate it inside a C++ object. Here's an example:
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;
};
To use it, you would just create a subclass of MyThreadClass with the InternalThreadEntry() method implemented to contain your thread's event loop. You'd need to call WaitForInternalThreadToExit() on the thread object before deleting the thread object, of course (and have some mechanism to make sure the thread actually exits, otherwise WaitForInternalThreadToExit() would never return)
The above answers are good, but in my case, 1st approach that converts the function to be a static didn't work. I was trying to convert exiting code to move into thread function but that code had lots to references to non-static class members already. The second solution of encapsulating into C++ object works, but has 3-level wrappers to run a thread.
I had an alternate solution that uses existing C++ construct - 'friend' function, and it worked perfect for my case. An example of how I used 'friend' (will use the above same example for names showing how it can be converted into a compact form using friend)
class MyThreadClass
{
public:
MyThreadClass() {/* empty */}
virtual ~MyThreadClass() {/* empty */}
bool Init()
{
return (pthread_create(&_thread, NULL, &ThreadEntryFunc, this) == 0);
}
/** Will not return until the internal thread has exited. */
void WaitForThreadToExit()
{
(void) pthread_join(_thread, NULL);
}
private:
//our friend function that runs the thread task
friend void* ThreadEntryFunc(void *);
pthread_t _thread;
};
//friend is defined outside of class and without any qualifiers
void* ThreadEntryFunc(void *obj_param) {
MyThreadClass *thr = ((MyThreadClass *)obj_param);
//access all the members using thr->
return NULL;
}
Ofcourse, we can use boost::thread and avoid all these, but I was trying to modify the C++ code to not use boost (the code was linking against boost just for this purpose)
You can't do it the way you've written it because C++ class member functions have a hidden this
parameter passed in. pthread_create()
has no idea what value of this
to use, so if you try to get around the compiler by casting the method to a function pointer of the appropriate type, you'll get a segmetnation fault. You have to use a static class method (which has no this
parameter), or a plain ordinary function to bootstrap the class:
class C
{
public:
void *hello(void)
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
static void *hello_helper(void *context)
{
return ((C *)context)->hello();
}
};
...
C c;
pthread_t t;
pthread_create(&t, NULL, &C::hello_helper, &c);
You'll have to give pthread_create
a function that matches the signature it's looking for. What you're passing won't work.
You can implement whatever static function you like to do this, and it can reference an instance of c
and execute what you want in the thread. pthread_create
is designed to take not only a function pointer, but a pointer to "context". In this case you just pass it a pointer to an instance of c
.
For instance:
static void* execute_print(void* ctx) {
c* cptr = (c*)ctx;
cptr->print();
return NULL;
}
void func() {
...
pthread_create(&t1, NULL, execute_print, &c[0]);
...
}