我需要将多个参数传递给我想在一个单独的线程上调用的函数.我已经读过,执行此操作的典型方法是定义结构,向函数传递指向该结构的指针,并为参数取消引用它.但是,我无法让它工作:
#include <stdio.h>
#include <pthread.h>
struct arg_struct {
int arg1;
int arg2;
};
void *print_the_arguments(void *arguments)
{
struct arg_struct *args = (struct arg_struct *)args;
printf("%d\n", args -> arg1);
printf("%d\n", args -> arg2);
pthread_exit(NULL);
return NULL;
}
int main()
{
pthread_t some_thread;
struct arg_struct args;
args.arg1 = 5;
args.arg2 = 7;
if (pthread_create(&some_thread, NULL, &print_the_arguments, (void *)&args) != 0) {
printf("Uh-oh!\n");
return -1;
}
return pthread_join(some_thread, NULL); /* Wait until thread is finished */
}
Run Code Online (Sandbox Code Playgroud)
这个输出应该是:
5
7
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,我实际得到: …
我有这些结构:
struct Request {
char buf[MAXLENREQ];
char inf[MAXLENREQ]; /* buffer per richiesta INF */
int lenreq;
uint16_t port; /* porta server */
struct in_addr serveraddr; /* ip server sockaddr_in */
char path[MAXLENPATH];
/*struct Range range;*/
};
struct RequestGet {
char buf[MAXLENREQ];
int maxconnect;
struct Range range;
};
struct ResponseGet{
char buf[MAXLENDATA];
//int LenRange;
int expire;
char dati[MAXLENDATA];
struct Range range;
};
Run Code Online (Sandbox Code Playgroud)
我怎么能把它们传递给pthread_create?无论每个结构领域的含义如何.
pthread_create(&id,NULL,thread_func,????HERE????);
Run Code Online (Sandbox Code Playgroud)