0

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_create(&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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
DriverBoy
  • 937
  • 1
  • 11
  • 23

2 Answers2

1

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()?

Community
  • 1
  • 1
Ralph
  • 5,154
  • 1
  • 21
  • 19
  • @Ralph,This cast is also causing a warning. – DriverBoy May 19 '11 at 05:35
  • 1
    Could you please post the warning you are getting? – Ralph May 19 '11 at 05:46
  • @Ralph,`warning: cast to pointer from integer of different size` – DriverBoy May 19 '11 at 05:56
  • @Ralph,that kind of hack will only work for 64 bit machine,right? – DriverBoy May 19 '11 at 06:31
  • Well, the number of Fs depends on the size of pointers on your system. You can get it by "sizeof(void*)". On a 32bit system (i.e. 4 bytes), you need to do 0xFFFFFFFF & sock_client. But anyhow - this is dirty, because you are not using pthread_create as it was intended. It should be used with a pointer to a data struct. – Ralph May 19 '11 at 06:35
  • @Ralph,how can I pass the `sock_client` to `proxy_handlereq` using `pthread_create` as it was intended? – DriverBoy May 19 '11 at 07:00
  • @Ralph,that example doesn't work for me,as the `sock_client` in my case is a temporary value of the main thread,which will be changed when new connections are `accepted`.. – DriverBoy May 19 '11 at 09:59
  • @DriverBoy: I think you need to get your design straight first, before worrying about compiler warnings. I guess, the intention is that each thread which you launch should serve one client socket? You must copy the contents of the main thread's sock_client to a data structure before the main thread accepts a new connection. This data structure you can pass to proxy_handlereq function as a void*. The first thing proxy_handlereq will do is, to cast it back to its original type. – Ralph May 24 '11 at 01:50
0

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().

Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
  • I don't think you can do this,`sock_client` will be changed when new connection are coming,`sock_client = accept(sock_listen, NULL, NULL)` – DriverBoy May 19 '11 at 06:35