Reputation: 9183
I know the reason of such error. "int" is not large enough to hold bytes of "void *". But my question is that how should I then handle pthread_create
of Linux, when I want to pass a function's argument for it to be executed. In many examples, I see such function forward-declaration:
void* thread_proc(void* arg);
And then in the function, a file-pointer is passed as arg
(which is of type int). But the compiler (logically) throws an error which warns against the casting of int to void. I even have used the intptr_t (aka long pointer) but to no avail.
result = pthread_create(&thread_id, NULL, thread_proc, (void *)new_request_socket);
new_request_socket
is an int representing the fd of a socket.
How the I can pass an integer as an argument of a function is itself passed as function to be executed by pthread_create()
.
Upvotes: 0
Views: 311
Reputation: 3677
You are probably passing int itself not pointer to the int.
You should actually pass pointer
result = pthread_create(&thread_id, NULL, thread_proc, (void *)&new_request_socket);
And in thread_proc(void *arg)
use it as such:
void thread_proc(void *arg)
{
int new_request_socket = *arg; //please put appropriate cast
}
EDIT: To clarify what Sam and Lightness points about passing the pointer thread_proc
:
new_request_socket
should be streamlined using locksnew_request_socket
until thread_proc
reads it.Essentially making new_request_socket
a pointer you will have to write more code. So dont write more code and use std::thread
:)
Upvotes: 1