pthread code example
snippet in c

pthread code example


// C program to show thread functions 
#include <pthread.h> 
#include <stdio.h> 
#include <stdlib.h> 
void* func(void* arg) 
    // detach the current thread 
    // from the calling thread 
    printf("Inside the thread\n"); 
    // exit the current thread 
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"); 
    // The following line terminates 
    // the thread manually 
    // pthread_cancel(ptid); 
    // Compare the two threads created 
    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"); 
// Driver code 
int main() 
    return 0;