0

I have created an array of structs. I am attempting to pass one element in this array to a thread through pthread_create. I am getting the following errors (they are for the 2 pthread_create calls I have in for loops):

/../a2/main.cpp|117|error: invalid conversion from ‘void (*)(serverDataStruct*)’ to ‘void* (*)(void*)’|

/../a2/main.cpp|117|error:   initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’|

I have looked all over and it seems that I have the correct syntax. I will post my code below. Can someone please help me?

struct serverDataStruct
{

};

struct clientDataStruct
{

};

void serverFunc(serverDataStruct *serverData);

void clientFunc(int *ticketNum);


struct serverDataStruct serverDataArray[MAX_NUM_SERVERS];   
struct clientDataStruct clientDataArray[MAX_NUM_CLIENTS]; 

int main (  )
{
    for(int i = 0; i < numServers; i++) //create servers
    {
        pthread_create( &serverTID[i], NULL, serverFunc, (void*) &serverDataArray[i]); //PROBLEM LINE*****************************************************************

    }

    for(int i = 0; i < numCustomers; i++)
    {
        pthread_create( &clientTID[i], NULL, clientFunc, (void*) &clientDataArray[i]); //PROBLEM LINE*****************************************************************

    }
}


void *serverFunc(void *serverData)
{

}


void *clientFunc(void *clientData)
{

}
2
  • you should add 4 spaces to make it look better Commented Feb 6, 2011 at 3:39
  • You will get a better response from the awesome SO community if you (1) narrow your code down to 10 or so lines that give the same error and (2) use the {} button to format your code neatly. Commented Feb 6, 2011 at 3:39

2 Answers 2

1

Your functions are defined correctly, but your forward declarations are incorrect.

Change these two lines:

void serverFunc(serverDataStruct *serverData);
void clientFunc(int *ticketNum);

to match the function definitions:

void *serverFunc(void *serverData);
void *clientFunc(void *ticketNum);

Btw, please edit your question to remove all the unnecessary code :-) And format it please :-)

Sign up to request clarification or add additional context in comments.

5 Comments

k, I've removed most of the unrelated code. and thanks, guess I needed a fresh set of eyes to see that mistake. another question, but now I am getting an error of the following format:
woops, continuing that previous comment: "undefined reference to `sem_open'", and I get this for all of my pthread functions (_create, _mutex, etc) and semaphore usages. Any thoughts?
@Robin: What headers are you including? I would suggesting accepting this answer and opening a new question for the sem_open issue. When you post that question, try not to post more than 10 lines of code. It should be trivial to reproduce your error in 10-15 lines of code, and you might even solve your issue in the process. My suggestion would be to copy your file and keep deleting things until you have a very small amount of code that gives you the problem still.
will do. thanks Jared. this was my first post so just learning the proper etiquette for posting.
@Robin: No need to apologize, we're here to help.
0

Try this:

Pthread func::

void serverFunc(void * serverData){
struct clientDataStruct * client = (struct clientDataStruct *) serverData; // just as an //example
....
}

pthread_create call:

pthread_create( &serverTID[i], NULL, serverFunc, &serverDataArray[i]);

If you are using c++ code: include this at the begining of you code after including all headers and namespaces , to prevent warnings:

extern "C"{void * serverFunc(void *);}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.