【发布时间】:2017-02-18 07:46:47
【问题描述】:
我知道这个错误的原因。 “int”不够大,无法容纳“void *”的字节。但我的问题是,当我想传递函数的参数以使其执行时,我应该如何处理 Linux 的pthread_create。在许多示例中,我看到了这样的函数前向声明:
void* thread_proc(void* arg);
然后在函数中,一个文件指针作为arg(它的类型为int)传递。但是编译器(逻辑上)会抛出一个错误,警告不要将 int 强制转换为 void。我什至使用过 intptr_t(又名长指针)但无济于事。
result = pthread_create(&thread_id, NULL, thread_proc, (void *)new_request_socket);
new_request_socket 是一个表示套接字 fd 的 int。
我如何将整数作为函数的参数传递,它本身作为函数传递给pthread_create()。
【问题讨论】:
-
你不需要处理
pthread_create。您只需使用std::thread。问题解决了。 -
将指针传递给
int而不是直接传递int值。 -
@SamVarshavchik 你能详细说明一下吗?
-
有什么要详细说明的? “使用
std::thread并避免整个问题”的哪一部分不清楚?