我想將兩個參數傳遞給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