這是代碼片段。我可以設置線程的名稱。但是,檢索線程的名稱時出現錯誤。請幫忙。爲什麼pthread_getname_np()在Linux的線程中失敗?
void *Thread_Function_A(void *thread_arg)
{
char buf[7];
int rc;
pthread_t self;
self = pthread_self();
rc = pthread_getname_np(self, buf,7);
if (rc != 0)
cout<<"Failed getting the name"<<endl;
}
int main(int argc, char *argv[])
{
int rc;
pid_t thread_pid_val = getpid();
thread_1.create_thread((thread_1.get_thread_id()), NULL,Thread_Function_A,&thread_pid_val);
thread_2.create_thread((thread_2.get_thread_id()), NULL,Thread_Function_A,&thread_pid_val);
rc = pthread_setname_np(*(thread_1.get_thread_id()), "Thread_A");
if(rc != 0)
{
cout<<"Setting name for thread A failed"<<endl;
}
rc = pthread_setname_np(*(thread_2.get_thread_id()), "Thread_B");
if(rc != 0)
{
cout<<"Setting name for thread B failed"<<endl;
}
pthread_join(*(thread_1.get_thread_id()), NULL);
pthread_join(*(thread_2.get_thread_id()), NULL);
return 0;
}
輸出: -
$./thread_basic.out
Failed getting the nameFailed getting the name
The name of thread is The name of thread is
的字符串錯誤說 - 數值結果超出範圍 誤差= 34
新增現在完整代碼。在這裏,我沒有得到正確的名字。相反,它會檢索程序的名稱。
void *Thread_Function_A(void *thread_arg)
{
char name[300];
char buf[200];
int rc;
char message[100];
FILE *fp;
pthread_t self;
self = pthread_self();
rc = pthread_getname_np(self, buf,200);
if (rc != 0)
{
cout<<"Failed getting the name"<<endl;
cerr<<"Pthread get name error ="<<rc<< " " << strerror(rc) << endl;
}
sprintf(name,"log_%s.txt",buf);
cout<<"The name of thread is "<<buf<<endl;
fp = fopen(name,"w+");
for(int i = 1; i<=5; i++)
{
sprintf(message,"The thread id is %d and value of i is %d",pthread_self(),i);
fprintf(fp,"%s\n", message);
fflush(fp);
/** local variable will not be shared actually**/
/** each thread should execute the loop for 5 **/
/** total prints should be 10 **/
}
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
int rc;
pthread_t threadA, threadB;
pid_t thread_pid_val = getpid();
thread_1.create_thread(&threadA, NULL,Thread_Function_A,&thread_pid_val);
thread_1.set_thread_id(threadA);
rc = pthread_setname_np(threadA, "Thread_A");
if(rc != 0)
{
cout<<"Setting name for thread A failed"<<endl;
}
thread_2.create_thread(&threadB, NULL,Thread_Function_A,&thread_pid_val);
thread_2.set_thread_id(threadB);
rc = pthread_setname_np(threadB, "Thread_B");
if(rc != 0)
{
cout<<"Setting name for thread B failed"<<endl;
}
pthread_join(threadA, NULL);
pthread_join(threadB, NULL);
return 0;
}
輸出如下。
]$ ./thread_basic.out
The name of thread is thread_basic.ou
The name of thread is Thread_B
如果您閱讀[手冊頁](http://man7.org/linux/man-pages/man3/pthread_setname_np.3.html),您將看到該函數返回錯誤號(而不是將其設置爲'errno'),你可能想用['strerror'](http://en.cppreference.com/w/c/string/byte/strerror)打印那個(或者可打印的字符串)。 –
超出範圍的數字結果是錯誤。 –
閱讀我之前評論中鏈接到的手冊頁。 –