我的任务是对打印服务器进行精简的多线程模型。
函数get_server的原型是:
void *get_request(void *arg);
“参数 arg 指向一个打开的文件描述符,从该描述符中读取请求。” 所以在测试中推荐使用 STDIN_FILENO,但是当一切都说完之后,描述符需要是通用的。
pthread_create(&tid, &attr, get_request, STDIN_FILENO);
在我试图使用 arg 的函数内部,不能将它从 void * 更改为任何可用的东西。例如,这些都不起作用:
read(*arg, intvariable, sizeof(int)); // can't cast void * error
int fd = *arg; // can't cast void * error
int fd = *(int *)arg; // seg fault
int fd = *((int *)arg); // seg fault
int fd = atoi(arg); // seg fault
// yes I'm aware arg isn't a char* but that's
// from the example code we were given