这是代码片段。我能够设置线程的名称。但是,在检索线程名称时出现错误。请帮忙。
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
strerror 说 - 数值结果超出范围 错误=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
最佳答案
除了竞争条件,它不会让你的调用失败但可能不会返回你想要的,这就是调用失败的原因。
man 3 pthread_getname_np
The pthread_getname_np() function can be used to retrieve the name of the thread. The thread argument specifies the thread whose name is to be retrieved. The buffer name is used to return the thread name; len specifies the number of bytes available in name. The buffer specified by name should be at least 16 characters in length. The returned thread name in the output buffer will be null terminated.
char buf[7];
会失败。
关于c++ - 为什么 pthread_getname_np() 在 Linux 的线程中失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20975829/