pthread_self() — Get the caller

Standards

Standards / Extensions C or C++ Dependencies

POSIX.4a
Single UNIX Specification, Version 3

both

POSIX(ON)

Format

#define _OPEN_THREADS
#include <pthread.h>

pthread_t pthread_self(void);

General description

Returns the thread ID of the calling thread.

Returned value

There are no documented errno values. Use perror() or strerror() to determine the cause of the error.

Example

CELEBP47
⁄* CELEBP47 *⁄                                   
#define _OPEN_THREADS                                                           
#include <pthread.h>                                                            
#include <stdio.h>                                                              
                                                                                
pthread_t thid, IPT;                                                            
                                                                                
void *thread(void *arg) {                                                       
  if (pthread_equal(IPT, pthread_self()))                                       
    puts("the thread is the IPT...?");                                          
  else                                                                          
    puts("the thread is not the IPT");                                          
                                                                                
  if (pthread_equal(thid, pthread_self()))                                      
    puts("the thread is the one created by the IPT");                           
  else                                                                          
    puts("the thread is not the one created by the IPT...?");                   
}                                                                               
                                                                                
main() {                                                                        
  IPT = pthread_self();                                                         
  if (pthread_create(&thid, NULL, thread, NULL) != 0) {                         
    perror("pthread_create() error");                                           
    exit(1);                                                                    
  }                                                                             
                                                                                
  if (pthread_join(thid, NULL) != 0) {                                          
    perror("pthread_create() error");                                           
    exit(3);                                                                    
  }                                                                             
}                                                                               
Output:
the thread is not the IPT
the thread is the one created by the IPT

Related information