public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
From: "Gardian, Milan" <Milan.Gardian@LEIBINGER.com>
To: Ye Liu <yliu@tibco.com>,
	pthreads-win32@sourceware.cygnus.com,
	pthreads-win32-info@sourceware.cygnus.com
Subject: RE: Using a class method as starting routine.
Date: Wed, 01 Aug 2001 01:39:00 -0000	[thread overview]
Message-ID: <4052AC502903D411AD1200508B2C0C1001F7EDFC@EXCHANGE> (raw)

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)".

             reply	other threads:[~2001-08-01  1:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-01  1:39 Gardian, Milan [this message]
  -- strict thread matches above, loose matches on Subject: below --
2001-07-31  7:25 Bossom, John
2001-07-30 13:50 Ye Liu
2001-07-30 20:06 ` Wayne Isaacs
2001-07-31 12:09   ` Ye Liu
2001-07-31  2:11 ` Ales Pour

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4052AC502903D411AD1200508B2C0C1001F7EDFC@EXCHANGE \
    --to=milan.gardian@leibinger.com \
    --cc=pthreads-win32-info@sourceware.cygnus.com \
    --cc=pthreads-win32@sourceware.cygnus.com \
    --cc=yliu@tibco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).