Robin
Robin

Reputation: 737

Passing struct from array of structs to pthread_create

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)
{

}

Upvotes: 0

Views: 1595

Answers (2)

Arunmu
Arunmu

Reputation: 6901

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 *);}

Upvotes: 0

JaredC
JaredC

Reputation: 5300

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 :-)

Upvotes: 1

Related Questions