Final answer:
To create 10 threads in C and print their unique IDs, you can use the pthread library. In this code, the function printID is the function every thread will run. Each thread prints its assigned ID.
Step-by-step explanation:
C Code to Create and Print IDs of 10 Threads
To create 10 threads in C and print their unique IDs, you can use the pthread library. Here's an example code:
#include <stdio.h>
#include <pthread.h>
void* printID(void* threadID) {
long tid = (long) threadID;
printf("Thread ID: %ld\\", tid);
pthread_exit(NULL);
}
int main() {
pthread_t threads[10];
int rc;
long t;
for (t = 0; t < 10; t++) {
rc = pthread_create(&threads[t], NULL, printID, (void*) t);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\\", rc);
return -1;
}
}
pthread_exit(NULL);
}
In this code, the function printID is the function every thread will run. The threadID passed as a parameter to each thread represents its unique ID. The main function creates 10 threads using a loop, passing the t variable as the thread ID. Each thread then prints its assigned ID. Finally, the program exits.