0% 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.

Uploaded by

api-594670325
Copyright
© © All Rights Reserved
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% 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.

Uploaded by

api-594670325
Copyright
© © All Rights Reserved
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
You are on page 1/ 1

// Name: Alex O'Brien

// Date: 7/26/22
// Title: threadHello_step1_Fixed
// Description: Fixed version of threadHello_step1, multi-threaded application
program

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *go(void *);


#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;
}

You might also like