Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

#include <stdio.

h>

#include <pthread.h>

/*thread function definition*/

void* threadFunction(void* args)

while(1)

printf("I am threadFunction.\n");

int main()

/*creating thread id*/

pthread_t tid;

int ret;

/*creating thread*/

ret=pthread_create(&tid,NULL,&threadFunction,NULL);

if(ret==0){

printf("Thread created successfully.\n");

else{

printf("Thread not created.\n");

return 0; /*return from main*/

while(1)

printf("I am main function.\n");

}
return 0;

Example2

//Printing thread IDs

#include <pthread.h>

#include <stdlib.h>

#include <stdio.h>

#include <unistd.h>

pthread_t ntid;

void printids(const char *s)

pid_t pid;

pthread_t tid;

pid = getpid();

tid = pthread_self();

printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,

(unsigned int)tid, (unsigned int)tid);

void * thr_fn(void *arg)

printids("new thread: ");

return((void *)0);

}
int main(void)

int err;

err = pthread_create(&ntid, NULL, thr_fn, NULL);

if (err != 0)

printf("can't create thread: %d\n", err);

printids("main thread:");

sleep(1);

exit(0);

You might also like