Skip to main content
gamedev.net
🔒 Locked

Linux and pthread programming

Started by DJHoy Nov 6, 2004 at 5:13 PM 2 replies 1.9k views
Original Post
DJHoy
DJHoy
I'm trying to write a small cross-platform multithreaded network piece, and I'm having some trouble with my call to pthread_create(). Basically, in the server class, I pass it the void* to a function that is called when a client socket connects. gcc keeps giving me the error: ANSI C++ forbids implicit conversion from `void *' in argument passing Here's come code snippits around where the problem lies:

#define NULL (0L)
#define BEGIN_THREAD(TH, FX, DT) pthread_create((pthread_t *)&TH, NULL, (void*)FX, (void*)DT)
#define HANDLE pthread_t *

HANDLE *clientHandles;
ClientObjectType **clientClassObjects;
void *connectFunction;

// ... the server run loop (abbreviated)...
void serverRunRoop(void *connectFunction) {
  int currentClient=0;
  clientHandles=new HANDLE[MAX_CLIENTS];
  clientClassObjects=new *ClientObjectType[MAX_CLIENTS];

  // ... lots of code snipped ...

  clientClassObject[currentClient]=new ClientObjectType();

  //error line is below...vvvv
  BEGIN_THREAD(clientHandle[currentClient], connectFunction, clientClassObjects[currentClient]);

  currentClient++; 

  //... more code here...

}

// when unraveled from the #defines, the begin thread looks like:
// pthread_create((pthread_t *)&clientHandle[currentClient], NULL, (void*)connectFunction, (void*)clientClassObjects[currentClient]);
//----

Any clue as to how to get around the error message above? I'm lost... thanks for any help!
oddbot
oddbot
The third arg to pthread_create is not a void *, its a:
void* (*start_routine)(void *)

So change your #define around a bit it should be ok.

-oddbot
MaulingMonkey
MaulingMonkey
Let's just fix this line:

pthread_create((pthread_t *)&clientHandle[currentClient], NULL, (void*)connectFunction, (void*)clientClassObjects[currentClient]);


Assuming "connectFunction" has a prototype like so:

void * connectFunction( void * );


You should be able to just write:

pthread_create((pthread_t *)&clientHandle[currentClient], NULL, connectFunction, (void*)clientClassObjects[currentClient]);


If it accepts or returns a different pointer type (BUT STILL TAKES 1 POINTER (which could be a pointer to a pointer) AND RETURNS A POINTER) you should be able to call like so:

int * connectFunction( char ** );pthread_create((pthread_t *)&clientHandle[currentClient], NULL, (void*(*)(void*))connectFunction, (void*)clientClassObjects[currentClient]);


Since I used a C-style cast, this should convert even if your function is declared like so:

bool connectFunction( int a , int b , int c );


But the behavior of this is so completely undefined that it will most likely crash in a very nasty way, since the number of bytes returned/passed don't even match up.
DJHoy
DJHoy
Quote:
Original post by MaulingMonkey
pthread_create((pthread_t *)&clientHandle[currentClient], NULL, (void*(*)(void*))connectFunction, (void*)clientClassObjects[currentClient]);

Since I used a C-style cast, this should convert even if your function is declared like so:


Groooooovy - that totally did it - the cast to (void*(*)(void*)) was exactly what was missing (and a couple of other flubs that I easily put back in place)... many thanks!



Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.