Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions threads-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define NUM_THREADS 4

// compile command
// gcc -pthread threads-1.c -o threads-1.out

void *PrintHello(void *threadid){
long tid;
tid = (long) threadid;
printf("Thread # %ld\n", tid);
pthread_exit(NULL);
}

int main(int argc, char *argv[]){
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t = 0; t < NUM_THREADS; t++){
printf("In main : creating thread no %ld\n",t);
rc = pthread_create(&threads[t],NULL,PrintHello, (void *)t);
if(rc){
printf("Error in making thread %d\n",rc);
exit(1);
}
pthread_join(threads[t],NULL); // Required to maintain async execution
}
pthread_exit(NULL);
return 0;
}