CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2010
    Posts
    1

    Talking Passing non member function pointer to pthread_create?

    Hi,

    I am having problem trying to pass down function pointer to a pthread_create, I have been searching around and apparently I have to declare a function extern "C" linkage and have it be "friend" with the class and then I could pass it down the pthread_create. However the compiler keep giving error "undefined reference to pthread_create".

    Anyway here is the code:

    #include <pthread.h>
    #include <string.h> /* for strerror() */
    #include <stdio.h>
    #include "ServerCommand.h"
    #include "CServerConnection.h"

    extern "C" { void *doWork(void* fd); }
    class CSocketThread {
    public:
    CSocketThread();
    virtual ~CSocketThread();

    int createThread();
    friend void *doWork(void* fd);
    int initiateServer();
    private:
    CServerConnection serverConnection ;
    pthread_t workerList[5];
    };

    And the cpp file:

    #include "CSocketThread.h"
    #include <pthread.h>
    #include <string.h> /* for strerror() */
    #include <stdio.h>


    void *doWork(void* fd)
    {
    int myFD = *(int *) fd;
    printf("thread 1 with file descriptor %d created successful",myFD);

    }


    CSocketThread::CSocketThread() {
    // TODO Auto-generated constructor stub
    }

    CSocketThread::~CSocketThread() {
    // TODO Auto-generated destructor stub
    }

    int CSocketThread::createThread(){
    int newFD = 0;
    newFD = serverConnection.acceptConnection();
    pthread_create(&workerList[0],NULL,doWork,&newFD); // error, undefined reference to pthread_create

    }


    Intuitively I know it's some silly mistake of mine, I just can't figure it out. So pls help

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Passing non member function pointer to pthread_create?

    Please use code tags!

    I haven't looked at the code, but if you're getting "undefined reference to pthread_create" then your problem isn't (necessarily) with your thread function prototype. What platform is this?

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Passing non member function pointer to pthread_create?

    Doesn't this:
    Code:
    extern "C" { void *doWork(void* fd); }
    Declare a variable called "doWork" that is of the type "function pointer" to a function that takes a void pointer?

    Viggy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured