how to install and run the simple code on MinGW through GCC for Pthread coding
Prerequisite: MinGW need to installed
Install GCC on MinGW : pacman -Syu gcc
After successful installation you will require to create the C file on notepad++ or in any of the file editor. Paste below mention code into the file and save it with .c extension.
Code: file name is hello.c
#include <omp.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *print_hello(void *thrd_nr) {
printf("Hello World. - It's me, thread #%ld\n", (long)thrd_nr);
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
printf(" Hello C code!\n");
const int NR_THRDS = omp_get_max_threads();
pthread_t threads[NR_THRDS];
for(int t=0;t<NR_THRDS;t++) {
printf("In main: creating thread %d\n", t);
pthread_create(&threads[t], NULL, print_hello, (void *)(long)t);
}
for(int t=0;t<NR_THRDS;t++) {
pthread_join(threads[t], NULL);
}
printf("After join: I am always last. Byebye!\n");
return EXIT_SUCCESS;
}
Compile and Run as follows
Linux: gcc -fopenmp -pthread hello.c && ./a.out
alternate way for linux
gcc -o hello -pthread hello.c
./hello
Windows MinGW: gcc -fopenmp -pthread hello.c && ./a.exe
Expected out put of the above code
Hello C code!
In main: creating thread 0
Hello World. - It's me, thread #0
In main: creating thread 1
Hello World. - It's me, thread #1
After join: I am always last. Byebye!Related link: https://stackoverflow.com/questions/7542286/how-to-run-pthreads-on-windows
Comments
Post a Comment