Unix Programming - class member function as the start function for pthread_create

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > July 2004 > class member function as the start function for pthread_create





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author class member function as the start function for pthread_create
googler

2004-07-18, 7:50 am

how to make a class member function as the start function for the
pthread_create

my code::

class thread
{
public :
void * run( void *);

void start( )
{
pthread_t tid;
pthread_create ( & tid, NULL, &run, NULL );
}
};

:The compiler complains that it cannot perform a cast from
void * (thread::*) (void * ) to void *(*)(void *)

joe@invalid.address

2004-07-18, 7:50 am

"googler" <arun_ccjl@yahoo.co.in> writes:

> how to make a class member function as the start function for the
> pthread_create


You can ususally get away with making it a static class member
function, but strictly speaking you should just define a free function
that takes a pointer to the object.

Joe
--
We can't all be heroes because someone has to sit on the curb and
clap as they go by.
- Will Rogers
Darko M.

2004-07-18, 5:55 pm

"googler" <arun_ccjl@yahoo.co.in> wrote in message news:<cddk7o$fqf@odbk17.prod.google.com>...
> how to make a class member function as the start function for the
> pthread_create
>
> my code::
>
> class thread
> {
> public :
> void * run( void *);
>
> void start( )
> {
> pthread_t tid;
> pthread_create ( & tid, NULL, &run, NULL );
> }
> };
>
> :The compiler complains that it cannot perform a cast from
> void * (thread::*) (void * ) to void *(*)(void *)


The problem is that the C++ class methods have a hidden argument,
which is the pointer to the structure in the name of which the
function executes ("this"). This is true in case the function is not
static. You could try making run() be static method, and then try
again.

This problem is pretty often, since pthread library is a C interface,
not C++.

Cheers
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com