From 73132fc2500cf4d7410a74741c7267349c5803d0 Mon Sep 17 00:00:00 2001 From: Sukrit Date: Thu, 10 Feb 2022 10:57:34 +0530 Subject: [PATCH] Threads --- threads-1.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 threads-1.c diff --git a/threads-1.c b/threads-1.c new file mode 100644 index 0000000..9cf4830 --- /dev/null +++ b/threads-1.c @@ -0,0 +1,32 @@ +#include +#include +#include + +#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; +} \ No newline at end of file