Write C program to create a thread using pthreads library and let it run its function

Ram Pothuraju
Creating a (Default) Thread

Use the function pthread_create() to add a new thread of control to the current process. It is prototyped by:

int pthread_create(pthread\_t *tid, const pthread\_attr\_t *tattr,
void*(*start_routine)(void *), void *arg);

When an attribute object is not specified, it is NULL, and the default thread is created with the following attributes:
•It is unbounded
•It is nondetached
•It has a a default stack and stack size
•It inhetits the parent's priority


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

void *mythread(void *vargp)
{
  sleep(1);
  printf("welcome to IM CSEIAN ·\n");
  return NULL;
}
int main()
{
  pthread_t tid;
  printf("before thread\n");
  pthread_create(&tid,NULL,mythread,NULL);
  pthread_join(tid,NULL);
  exit(0);
}

Out Put :: 

To Compile 

cc filename.c –l pthread

To Run
./a.out


Welcome to IM CSEIAN ·



Post a Comment

0Comments

Post a Comment (0)