我想将两个参数传递给C中的线程。我创建了一个数组(大小为2),并试图将该数组传递给线程。这是将多个参数传递给线程的正确方法吗?如何将多个参数传递给C中的线程
// parameters of input. These are two random numbers
int track_no = rand()%15; // getting the track number for the thread
int number = rand()%20 + 1; // this represents the work that needs to be done
int *parameters[2];
parameters[0]=track_no;
parameters[1]=number;
// the thread is created here
pthread_t server_thread;
int server_thread_status;
//somehow pass two parameters into the thread
server_thread_status = pthread_create(&server_thread, NULL, disk_access, parameters);
检查你的代码,您声明指针数组为int和分配他们与int值。 – Teudimundo
我注意到了这个警告。如果参数不是指针而只是一个数组是否合法? –
如果将参数声明为int(“int parameter [2];”)的数组,则可以将参数作为指针传递。它是指向第一个int的指针。然后可以以线程的形式作为数组访问它。 – Teudimundo