Thread functions in C/C++

CC++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to understand thread functions in C/C++.

Thread functions allow users to implement concurrent functions at the same time, which can either be dependent on each other for execution or independent.


#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void* func(void* arg){
   //detaching the current thread
   printf("Inside the thread\n");
void fun(){
   pthread_t ptid;
   //creating a new thread
   pthread_create(&ptid, NULL, &func, NULL);
   printf("This line may be printed before thread terminates\n");
   if(pthread_equal(ptid, pthread_self())
      printf("Threads are equal\n");
      printf("Threads are not equal\n");
   //waiting for the created thread to terminate
   pthread_join(ptid, NULL);
   printf("This line will be printed" " after thread ends\n");
int main(){
   return 0;


This line may be printed before thread terminates
Threads are not equal
Inside the thread
This line will be printed after thread ends
Published on 02-Mar-2020 15:45:48