1、pthread_setname_np:设置线程名称
#include <pthread.h>
int pthread_setname_np(pthread_t thread, const char *name);
参数一:需要设置名称的线程
参数二:需要设置名称的buffer,要求name的buffer空间不能超过16个字节,否则报错ERANGE.
eg. pthread_setname_np(pthread_self(), task->name);
2、pthread_self:获取线程id,其作用对应进程中getpid()函数
#include <pthread.h>
pthread_t pthread_self(void);
3、pthread_setcancelstate:对cancel信号的反应
int pthread_setcancelstate(int state, int *oldstate);
state有两种值:PTHREAD_CANCEL_ENABLE(缺省)和PTHREAD_CANCEL_DISABLE
old_state如果不为NULL,则存入原来的cancel状态以便恢复;
old_state如果为NULL,则不存原cancel状态。
eg. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
4、sysconf():获取处理器核数
#include <unistd.h>
其参数可以是_SC_NPROCESSORS_CONF或_SC_NPROCESSORS_ONLN
sysconf(_SC_NPROCESSORS_CONF)返回系统可以使用的核数,但其值会包括系统中禁用的核的数目,因此该值并不代表当前系统中可用的核数。
sysconf(_SC_NPROCESSORS_ONLN)的返回值真正代表了系统当前可用的核数。
eg. cpuIdx = sysconf(_SC_NPROCESSORS_CONF) - 2;
5、CPU_ISSET:判断某个cpu是否已在cpu集中设置
int CPU_ISSET (int cpu, const cpu_set_t *set);
cpu集可以认为是一个掩码,每个设置的位都对应一个可以合法调度的cpu,而未设置的位则对应一个不可调度的cpu。换而言之,线程都被绑定了,只能在那些对应位被设置了的处理器上运行。通常,掩码中的所有位都被置位了,也就是可以在所有的cpu中调度。结构体cpu_set_t可以理解为cpu集。
eg. CPU_ISSET(cpu, &excl_cpuset);
6、CPU_ZERO:初始化,设为空
void CPU_ZERO (cpu_set_t *set);
eg. CPU_ZERO(&cpuset);
7、CPU_SET:将某个cpu加入cpu集中
void CPU_SET(int cpu, cpu_set_t *set);
eg. CPU_SET(cpu, &cpuset);
8、sched_setaffinity:设置进程的CPU亲和力
使进程绑定在某一个或几个CPU上运行,避免在CPU之间来回切换,从而提高该进程的实时性能。
eg. if (sched_setaffinity(0, sizeof(cpu_set_t), &cpuset) == -1)