工程地址
https://github.com/jiafeng-1/thread_tutorials.git
获取调用者的线程ID
获取线程id,常常在线程中设置线程分离使用。
pthread_t pthread_self(void);
线程名称
int pthread_setname_np(pthread_t thread, const char *name);
int pthread_getname_np(pthread_t thread, char *name, size_t len);
线程与cpu的亲和性(affinity)
设置线程与cpu的亲和性其实就是我们常说的绑核。绑核并不意味着该线程独占这个cpu核,其他线程或者进程也是可以在上面运行的。
int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,const cpu_set_t *cpuset);
int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize, cpu_set_t *cpuset);
//初始化,设为空
void CPU_ZERO (cpu_set_t *set);
//将某个cpu加入cpu集中
void CPU_SET (int cpu, cpu_set_t *set);
//将某个cpu从cpu集中移出
void CPU_CLR (int cpu, cpu_set_t *set);
//判断某个cpu是否已在cpu集中设置了
int CPU_ISSET (int cpu, const cpu_set_t *set);
#include <stdio.h>
#define __USE_GNU
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>
int GetCpuCount()
{
return (int)sysconf(_SC_NPROCESSORS_ONLN);
}
void *thread_fun_1()
{
pthread_setname_np(pthread_self(), "thread_1");
char thread_name[20] = {0};
while(1)
{
pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name));
printf("the thread_1's Pid is %d\n", getpid());
printf("The LWPID/tid of thread_1 is: %ld\n", (long int)gettidv1());
printf("thread name = %s\n", thread_name);
printf("thread name = %s\n", thread_name);
printf("thread name = %s\n", thread_name);
printf("thread name = %s\n", thread_name);
printf("thread name = %s\n", thread_name);
sleep(1);
}
return NULL;
}
void *thread_fun_2()
{
pthread_setname_np(pthread_self(), "thread_2");
char thread_name[20] = {0};
while(1)
{
pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name));
printf("the thread_1's Pid is %d\n", getpid());
printf("The LWPID/tid of thread_1 is: %ld\n", (long int)gettidv1());
printf("thread name = %s\n", thread_name);
printf("thread name = %s\n", thread_name);
printf("thread name = %s\n", thread_name);
printf("thread name = %s\n", thread_name);
printf("thread name = %s\n", thread_name);
printf("thread name = %s\n", thread_name);
sleep(1);
}
return NULL;
}
int main()
{
int cpu_num = 0;
cpu_num = GetCpuCount();
printf("The number of cpu is %d\n", cpu_num);
pthread_t t1;
pthread_t t2;
pthread_attr_t attr1;
pthread_attr_t attr2;
pthread_attr_init(&attr1);
pthread_attr_init(&attr2);
if (0!=pthread_create(&t1, &attr1, thread_fun, NULL))
{
printf("create thread 1 error\n");
return;
}
if (0!=pthread_create(&t2, &attr2, thread_fun, NULL))
{
printf("create thread 2 error\n");
return;
}
cpu_set_t cpu_info;
CPU_ZERO(&cpu_info);
CPU_SET(0, &cpu_info);
if (0!=pthread_setaffinity_np(t1, sizeof(cpu_set_t), &cpu_info))
{
printf("set affinity failed");
}
CPU_ZERO(&cpu_info);
CPU_SET(1, &cpu_info);
if (0!=pthread_setaffinity_np(t2, sizeof(cpu_set_t), &cpu_info))
{
printf("set affinity failed");
}
pthread_join(t1, NULL);
pthread_join(t2, NULL);
}
编译
gcc thread.c -o thread_bind_cpu -lpthread
查看线程是否都在一个核心上
ps -eLF | grep thread_bind_cpu
jiafeng 681 129096 681 0 3 5773 808 10 19:11 pts/7 00:00:00 ./main/thread_bind_cpu
jiafeng 681 129096 682 16 3 5773 808 0 19:11 pts/7 00:00:52 ./main/thread_bind_cpu
jiafeng 681 129096 683 16 3 5773 808 1 19:11 pts/7 00:00:52 ./main/thread_bind_cpu
可以看到,681进程中的 681主线程在#10核心上,682线程在#0核心上,683线程在#1核心上