threading a class member function

threading a class member function

Post by Ricoche » Sun, 11 Nov 2001 15:59:35



I am able to thread a static member function... but was wondering if anyone
knew if I could thread a normal member function.

class A {

    void *function( void * );
    void *function2( void * );

Quote:};

void *A::function( void *args )
{ ...... }

void *A::function2( void *args )
{
    pthread_t tid;
    pthread ( %tid, NULL, ????, NULL );

Quote:}

 
 
 

threading a class member function

Post by red floy » Sun, 11 Nov 2001 16:21:36


You can't use a regular member function as a thread function because it has
an implied "this" pointer.

Why not pass "this" as the parameter to the static membe function in the
thread, and then invoke the non-static from the thread?

i.e.

class foo {
//...
    static void *ThreadFunc(void *)
    void *MemberFunc(void *);

Quote:}

void *foo::ThreadFunc(void *p)
{
    foo* pObj = static_cast<foo*>(p);
    return pObj->MemberFunc(some_parameter_here);

}



Quote:> I am able to thread a static member function... but was wondering if
anyone
> knew if I could thread a normal member function.

> class A {

>     void *function( void * );
>     void *function2( void * );

> };

> void *A::function( void *args )
> { ...... }

> void *A::function2( void *args )
> {
>     pthread_t tid;
>     pthread ( %tid, NULL, ????, NULL );
> }


 
 
 

1. C++ class member function as a thread function.

Hi,

  I tried to create a C++ class, one of the member functions is set as a
thread function. For example:

class cc
{
  public:
    void* ThreadFunc(void* p);

void* cc::ThreadFunc(void *p)
{
  char *s;

  s=(char*)p;
  printf("%s\n", s);

main()
{
  cc myCC;
  char szHello[] = "Hello!";

  pthread_create(&th, NULL, myCC.ThreadFunc, (void*)szHello);

  pthread_join(th, NULL);

For some reason, the pointer 'p' in ThreadFunc does not point to the same
address as the one passed via pthread_create. I don't know what I am doing
wrong here.

Any help is appreciated.

- Joe

2. Weird problem with BOCABOARD 2016...

3. Make a class member fit a C function Sig?

4. ersh script info

5. Function pointer to member functions

6. Olivetti DSM 60-400

7. problem running Java Applets on linux - threads: cannot find class java/lang/Thread ????

8. OpenLDAP or SUN ONE DS5.2 HOW-TO updated with sudo+LDAP and apache+LDAP info

9. g++ - Problem with static class member.

10. Is it possible to use C++ class member in Signal?

11. C++: const int and const char* members in classes

12. Class member as MOTIF callbacks???

13. CXX 6.1-027 and pointer-to-member of private classes.