Function pointer to member functions

Discussion in 'C++' started by Rookie, Oct 12, 2004.

  1. Rookie

    Rookie Guest

    Hi,

    I was writing a simple multi-threaded program (code below). My intent is to
    invoke a member function of a class that creates two threads - each thread
    executes another member function of the same class. The pthread_create
    function used to create a thread requires a pointer to the function that
    will be executed. I tried all the possible syntax ideas that came to mind,
    but I am not able to pass it a pointer to the member function of the class.
    Can someone please point out my error. Thanks.

    class test
    {
    public:
    void* funcA(void* dud)
    {
    int i=0;
    char c,*d;
    d=(char*)dud;
    c=*d;
    for(i=0;i<100;i++)
    printf("%c",c);
    }

    int funcB()
    {
    pthread_t threadId1,threadId2;
    char c,d;
    c='a';
    pthread_create(&threadId1,NULL,this->funcA,&c); //I keep getting
    compilation errors for this line. I tried a::funcA, &a::funcA, funcA,
    &funcA. None of them work.
    d='b';
    pthread_create(&threadId2,NULL,this->funcA,&d); //I keep getting
    compilation errors for this line
    pthread_join(threadId2,NULL);
    pthread_join(threadId1,NULL);
    perror("say what");
    }
    };

    main()
    {
    test obj;
    obj.funcB();
    }
     
    Rookie, Oct 12, 2004
    #1
    1. Advertising

  2. Rookie

    DaKoadMunky Guest

    1. Advertising

  3. Rookie

    Lev Walkin Guest

    Rookie wrote:
    > Hi,
    >
    > I was writing a simple multi-threaded program (code below). My intent is to
    > invoke a member function of a class that creates two threads - each thread
    > executes another member function of the same class. The pthread_create
    > function used to create a thread requires a pointer to the function that
    > will be executed. I tried all the possible syntax ideas that came to mind,
    > but I am not able to pass it a pointer to the member function of the class.
    > Can someone please point out my error. Thanks.


    Your main problem is cross-posting.

    The second problem is that a funcA has a hidden "this" pointer, which is not
    passed through for you by pthread_create(). Use a "friend" wrapper, or declare
    the funcA as void*funcA(void) and have the "this" pointer passed explicitly:

    pthread_create(&threadId1, 0, (void (*)(void))this->funcA, this);

    You'll lose the ability to pass &c or &d pointer, though.

    > class test
    > {
    > public:
    > void* funcA(void* dud)
    > {
    > int i=0;
    > char c,*d;
    > d=(char*)dud;
    > c=*d;
    > for(i=0;i<100;i++)
    > printf("%c",c);
    > }
    >
    > int funcB()
    > {
    > pthread_t threadId1,threadId2;
    > char c,d;
    > c='a';
    > pthread_create(&threadId1,NULL,this->funcA,&c); //I keep getting
    > compilation errors for this line. I tried a::funcA, &a::funcA, funcA,
    > &funcA. None of them work.
    > d='b';
    > pthread_create(&threadId2,NULL,this->funcA,&d); //I keep getting
    > compilation errors for this line
    > pthread_join(threadId2,NULL);
    > pthread_join(threadId1,NULL);
    > perror("say what");
    > }
    > };
    >
    > main()
    > {
    > test obj;
    > obj.funcB();
    > }
    >
    >



    --
    Lev Walkin
     
    Lev Walkin, Oct 12, 2004
    #3
  4. Lev Walkin <> writes:

    > pthread_create(&threadId1, 0, (void (*)(void))this->funcA, this);


    The result of doing the above is still undefined.

    POSIX requires that an 'extern "C"' linkage function be passed to
    pthread_create().

    This has been discussed many times and at length in com.programming.threads

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.
     
    Paul Pluzhnikov, Oct 12, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    668
    John Carson
    Jul 30, 2003
  2. slide_o_mix
    Replies:
    0
    Views:
    446
    slide_o_mix
    Oct 15, 2003
  3. Fraser Ross
    Replies:
    4
    Views:
    1,098
    Fraser Ross
    Aug 14, 2004
  4. Stephen Howe
    Replies:
    2
    Views:
    310
    Stephen Howe
    Nov 6, 2012
  5. somenath
    Replies:
    2
    Views:
    180
    somenath
    Aug 29, 2013
Loading...

Share This Page