Tutorialspoint.dev

pthread_self() in C with Example

Prerequisite : Multithreading in C

Syntax :- pthread_t pthread_self(void);


The pthread_self() function returns the ID of the thread in which it is invoked.

// C program to demonstrate working of pthread_self()
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* calls(void* ptr)
{
    // using pthread_self() get current thread id
    printf("In funtion thread id = %d ", pthread_self());
    pthread_exit(NULL);
    return NULL;
}
  
int main()
{
    pthread_t thread; // declare thread
    pthread_create(&thread, NULL, calls, NULL);
    printf("In main thread id = %d ", thread); 
    pthread_join(thread, NULL); 
    return 0;
}



Output:

In function
thread id = 1
In main
thread id = 1

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

tags:

C C

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter