This is the mail archive of the pthreads-win32@sources.redhat.com mailing list for the pthreas-win32 project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: Using a class method as starting routine.


Hi Ye, pthread gurus,


> A rather dump question: how to use a class method as a 
> starting routine of pthread_create().

I agree with what John Bossom already wrote -> the Java-like approach is
simple and powerful. First, you should create a new abstract class that will
take care of the nasty details of delegation (alias proxying), e.g.:

//runnable.h
//----------
#include <assert.h>
#include <pthread.h>
extern "C" void *runnable_exec_redirector(void *arg);
class Runnable
{
    //Public interface
    public:
        bool start_thread();
        bool join_thread(void ** ret_value = 0);

        Runnable() : _id_valid(false) {};
        virtual ~Runnable() { assert( !_id_valid ); };


    //Methods that must be overriden in a subclass
    protected:
        virtual void *run() = 0;

        //allow this function to access class details and run method
        friend void *runnable_exec_redirector(void *arg);


    //Attributes, available to subclasses
    protected:
        bool      _id_valid;
        pthread_t _thread_id;
};
//----------(eof)


//runnable.cpp
//----------
#include "runnable.h"
bool Runnable::start_thread()
{
    if (!_id_valid) {
        if (0 == pthread_create(
          &_thread_id, 0,
          runnable_exec_redirector,
          static_cast<void *>(this)
        )) {
            _id_valid = true;
        }
    }
    return _id_valid;
}

bool Runnable::join_thread(void ** ret_value)
{
    bool ret = false;
    if (_id_valid) {
        pthread_join(_thread_id, ret_value);
        _id_valid = false;
        ret = true;
    }
    return ret;
}

extern "C" void *runnable_exec_redirector(void *arg)
{
    assert (arg != 0);
    Runnable *obj = static_cast<Runnable *>(arg);
    assert (obj != 0);
    return obj ? obj->run() : 0;
}
//----------(eof)




You can now reuse this code in as many thread classes as you want (of course
you can refine the above code to support re-entrancy and zillion other
features, this is just a skeleton). Let's take a look at an example:


//test.cpp
//----------
#include <iostream>
#include "runnable.h"
class MyThread : public Runnable
{
    private:
        virtual void *run()
        {
            std::cout
                << "I am thread " << _thread_id
                << " running in context of object " << this
                << std::endl;
            return 0;
        }
};

int main()
{
    MyThread t;
    std::cout << "Thread object " << &t << std::endl;

    t.start_thread();
    t.join_thread();

    return 0;
}
//----------(eof)


As you can see, all you have to do in your "thread" class (MyThread in the
example above) is to (usually publicly) derive from "Runnable" -> i.e. make
your class runnable ;). This gives your class instant access to the Runnable
public methods (start_thread, join_thread and whatever you can think of).

The only other think you need to do is to define & implement the 'run' pure
virtual method (otherwise you would not be able to create instances of your
class :) ). You can think of the 'run' method as the starting routine for
the created thread, and it is instance method, not a static method (and
that's what you wanted).

Of course, your question was how to use instance method DIRECTLY in
pthread_create -> it is NOT possible (search through history of
news://comp.programming.threads for detailed explanation). But this simple
'proxy' strategy is at least as good as the direct usage (in my opinion even
better).

If you have any further questions/problems, let me know.

Hope this helps,

	Milan.

PS: The above code was successfully tried in "VC++6, SP4" (alias "Microsoft
(R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804") and "gcc version
2.95.3-5 (cygwin special)".


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]