How can I get rid of this c warning?
Here sock_client is an socket:
LaunchThread(proxy_handlereq, sock_client);
static void LaunchThread(int (*func)(), void *parg)
{
#ifdef WINDOWS
LPDWORD tid;
CreateThread(NULL, 0L, (void *)func, parg, 0L, &tid);
#else
pthread_t pth;
pthread_crea开发者_Go百科te(&pth, NULL, func, parg);
#endif
}
I'm getting the following warning: warning: cast to pointer from integer of different size
How can I pass it as the 2nd parameter of LaunchThread
?
Try this:
LaunchThread(proxy_handlereq, (void*)sock_client);
Edit:
Ok, now I see: sock_client is just the integer number of the port. And you want to pass this number to the other thread, right?
(Depending on the pointer size on your system) you can get rid of the warning by this dirty cast:
LaunchThread(proxy_handlereq, (void*)(0xFFFFFFFFFFFFFFFF & sock_client);
But actually I would recommend, that you create a data structure with all the information, that you want to pass to the other thread, e.g.:
struct MyData {
int socket_no;
const char* host_name;
...
};
Then create an instance of this and pass a pointer to the instance to your LaunchThread function.
Edit2:
You can see some sample code in this question: Multiple arguments to function called by pthread_create()?
If sock_client
is a socket, you need to invoke LaunchThread as:
LaunchThread(proxy_handlereq, &sock_client);
because both CreateThread
and pthread_create
expect a pointer to the argument to pass on to func()
.
精彩评论