This is the mail archive of the
pthreads-win32@sources.redhat.com
mailing list for the pthreas-win32 project.
Re: Using a class method as starting routine.
- To: Wayne Isaacs <wisaacs at io dot com>, pthreads-win32 at sourceware dot cygnus dot com, pthreads-win32-info at sourceware dot cygnus dot com
- Subject: Re: Using a class method as starting routine.
- From: "Ye Liu" <yliu at tibco dot com>
- Date: Tue, 31 Jul 2001 12:06:33 -0700
- Organization: Tibco Software
- References: <3B65C795.A5680EC7@tibco.com> <000901c1196d$cd9ccea0$6601a8c0@stone>
Thanks, Wayne.
I found my problem later after I posted the mail. Now my class is like:
/* create.h */
#ifndef _CREATE_H_
#define _CREATE_H_
#include <stdio.h>
#include "pthread.h"
class create
{
public:
create();
static void *thread_routine(void *arg)
{
create *pObject = (create *)pObject;
...
}
void start();
};
#endif
/* create.cpp */
#include "create.h"
void *create::thread_routine(void *arg)
{
printf("The thread is here\n");
return NULL;
}
void create::start()
{
pthread_t thread_id;
pthread_attr_t thread_attr;
pthread_create(&thread_id, &thread_attr, create::thread_routine, (void *)this);
}
Pass this as the arg to the thread routine to enable it to access the
members/methods of the class: static function can only access the static
members/methods of the class. I feel it is an even better way in sense of Object
Oriented.
--ye
Wayne Isaacs wrote:
> I have never found a way to generate a usable function pointer from a
> non-static class method. It may be theoretically incorrect to even try,
> because a non-static class method should not exist independently from the
> data object.
>
> On the other hand, if you declare the method 'static', then that method
> exists independent of data. The compiler will let you generate and use a
> pointer to any static method.
>
> I would be interested to know Bjarne Stroustrup's preference on this issue,
> because the static function pointer required to launch thread routines is
> contrary to object orientation, but a fact of life for the major operating
> systems.
>
> I have written several thread classes. Usually, the class object carries a
> thread function pointer and a termination event in private data. The class
> defines the thread function pointer as a typedef. The thread function takes
> two parameters; a void pointer for data, and an event pointer for
> termination. That way, when the thread class object goes out of scope and
> destructs, it can gracefully terminate the thread.
>
> something like:
>
> class ThreadFn {
>
> pthread_t thread_id;
> HANDLE termination_event;
>
> public:
> typedef long (*static_f_type)( HANDLE, void* );
> ThreadFn( static_f_type );
>
> };
>
> Wayne
>
> ----- Original Message -----
> From: "Ye Liu" <yliu@tibco.com>
> To: <pthreads-win32@sourceware.cygnus.com>;
> <pthreads-win32-info@sourceware.cygnus.com>
> Sent: Monday, July 30, 2001 4:46 PM
> Subject: Using a class method as starting routine.
>
> > Greets,
> >
> > A rather dump question: how to use a class method as a starting routine
> > of pthread_create().
> >
> > I have the following code:
> > /* create.h */
> > #ifndef _CREATE_H_
> > #define _CREATE_H_
> >
> > #include <stdio.h>
> > #include "pthread.h"
> >
> > class create
> > {
> > public:
> > create();
> > void *thread_routine(void *arg);
> > void start();
> > };
> >
> > #endif
> >
> > /* create.cpp */
> > #include "create.h"
> >
> > void *create::thread_routine(void *arg)
> > {
> > printf("The thread is here\n");
> > return NULL;
> > }
> >
> > void create::start()
> > {
> > pthread_t thread_id;
> > pthread_attr_t thread_attr;
> >
> > pthread_create(&thread_id, &thread_attr, create::thread_routine, NULL);
> >
> > }
> >
> > create::create()
> > {
> > }
> >
> > When I compiled it using VC6.0 (win32-pthread library of course), I got
> >
> > create.cpp
> > C:\ye\work\vs\pthread_projects\create\create.cpp(14) : error C2664:
> > 'pthread_create' : cannot convert parameter 3 from 'void *(void *)' to
> > 'void *(__cdecl *)(void *)'
> > None of the functions with this name in scope match the target
> > type
> > Error executing cl.exe.
> >
> >
> > It seems that the compiler does not recognize thread_routine as a
> > address. Why does it happen? How to get around of it?
> >
> > Thanks a lot!
> >
> > --ye
> >
> >
> > --
> > Ye Liu
> > Tel(O) 650-846-5228
> >
> >
--
Ye Liu
Tel(O) 650-846-5228