在此范围内未声明"pthread_setname_np" [英] 'pthread_setname_np' was not declared in this scope

查看:278
本文介绍了在此范围内未声明"pthread_setname_np"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中创建了多个线程.我想为每个pthread分配一个名称,所以我使用了 pthread_setname_np ,它在Ubuntu上可用,但在SUSE Linux上不可用.

I have created multiple threads in my application. I want to assign a name to each pthread so I used pthread_setname_np which worked on Ubuntu but is not working on SUSE Linux.

我在Google上搜索了一下,才知道'_np'的意思是'不可移植的',并且该API并非在所有Linux操作系统上都可用.

I googled it and came to know '_np' means 'non portable' and this api is not available on all OS flavors of Linux.

所以现在我只想在API可用的情况下执行此操作.如何确定api是否可用?我需要这样的东西.

So now I want to do it only if the API is available. How to determine whether the api is available or not ? I need something like this.

#ifdef SOME_MACRO
    pthread_setname_np(tid, "someName");
#endif

推荐答案

您可以使用feature_test_macro _GNU_SOURCE 来检查此功能是否可用:

You can use the feature_test_macro _GNU_SOURCE to check if this function might be available:

#ifdef _GNU_SOURCE
    pthread_setname_np(tid, "someName");
#endif

但是手册指出 pthread_setname_np pthread_getname_np glibc 2.12 中引入.因此,如果您使用的是较旧的glibc(例如2.5),则定义 _GNU_SOURCE 将无济于事.

But the manual states that the pthread_setname_np and pthread_getname_np are introduced in glibc 2.12. So if you are using an older glibc (say 2.5) then defining _GNU_SOURCE will not help.

因此最好避免使用这些不可移植的函数,并且您可以轻松地命名线程作为线程创建的一部分,例如,使用线程 ID 和数组之间的映射,例如:

So it's best to avoid these non portable function and you can easily name the threads yourself as part of your thread creation, for example, using a map between thread ID and a array such as:

pthread_t tid[128];
char thr_names[128][256]; //each name corresponds to on thread in 'tid'

您可以使用以下命令检查glibc版本:

You can check the glibc version using:

getconf GNU_LIBC_VERSION

这篇关于在此范围内未声明"pthread_setname_np"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆