pthread_self() - 呼び出し元の取得

標準

標準/拡張機能 C/C++ 依存項目

POSIX.4a
Single UNIX Specification、バージョン 3

両方

POSIX(ON)

形式

#define _OPEN_THREADS
#include <pthread.h>

pthread_t pthread_self(void);

機能説明

呼び出しスレッドのスレッド ID を戻します。

戻り値

設定される errno 値はありません。 perror() または strerror() を使用して、エラーの原因を判別してください。

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);                                                                    
  }                                                                             
}                                                                               
出力:
the thread is not the IPT
the thread is the one created by the IPT

関連情報