The document describes a C program that creates 10 threads using pthreads and passes an integer value to each thread. Each thread prints a message with its thread ID and the passed integer value before returning. The main thread then waits for all threads to finish and ends.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
50 views1 page
Threadhello Step1 Fixed
The document describes a C program that creates 10 threads using pthreads and passes an integer value to each thread. Each thread prints a message with its thread ID and the passed integer value before returning. The main thread then waits for all threads to finish and ends.
#define NTHREADS 10 pthread_t threads[NTHREADS]; int main() { int i; for (i = 0; i < NTHREADS; i++) { int *pointer = malloc(sizeof(int)); //creating new variable to pass to thread during each iteration *pointer = i; pthread_create(&threads[i], NULL, go, pointer); } for (i = 0; i < NTHREADS; i++) { printf("Thread %d returned \n", i); pthread_join(threads[i], NULL); } printf("Main thread done.\n"); return 0; } void *go(void *arg) { printf("Hello from thread %d with iteration %d\n", (int)pthread_self(), *(int *)arg); free(arg); //freeing the pointer from memory return 0; }