Click to See Complete Forum and Search --> : Passing non member function pointer to pthread_create?


starian
May 20th, 2010, 11:36 PM
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

hoxsiew
May 21st, 2010, 07:40 AM
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?

MrViggy
May 21st, 2010, 03:42 PM
Doesn't this:
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