I try to compile a c file which was include threads. but i tried to compile normal way like this
gcc -o thread thread.c -Wall
But it gives an error. but I tried to compile like this way
gcc -pthread -o thread thread.c -Wall
It worked. what is the reason of this and -pthread flag what will do? my C code below
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
void *thread_function(void *arg)
{
int a;
for(a=0;a<10; a++)
{
printf("Thread says hi!\n");
sleep(2);
}
return NULL;
}
int main(void)
{
pthread_t mythread;
if ( pthread_create( &mythread, NULL, thread_function, NULL) )
{
printf("error creating thread.");
abort();
}
if ( pthread_join ( mythread, NULL ) )
{
printf("error joining thread.");
abort();
}
printf("Main thread says hi!\n");
exit(0);
}