多线程编程之线程绑定处理器核


Linux系统提供API函数sched_setaffinity和sched_getaffinity用于设置或获取线程的可以使用的CPU核。


int sched_setaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t
*mask);


这个函数中pid表示需要设置或获取绑定信息的线程id(或进程id),如果为0,表示对当前调用的线程进行设置;第2个参数cpusetsize一般设置为sizeof(cpu_set_t),用以表示第3个参数指向的内存结构对象的大小;第3个参数mask指向类型为cpu_set_t对象的指针,用以设置或获取指定线程可以使用的CPU核列表。Linux提供函数CPU_ZERO、CPU_SET和CPU_ISSET对cpu_set_t类型的对象进行操作,其中CPU_ZERO用于清空cpu_set_t类型对象的内容,CPU_SET用于设置cpu_set_t类型对象,CPU_ISSET用于判断cpu_set_t类型对象与核对应的位是否被设置。下面通过简单的代码示例来说明这两个函数的具体用法。


设置线程绑定代码:


cpu_set_t mask;


int blist[8]={2, 5, 13, 9, 3, 6, 7, 4}; //设置需要绑定的核列表


#pragma omp parallel private(mask)


{


         CPU_ZERO(&mask);


         CPU_SET(blist[omp_get_thread_num()], &mask); //对每个线程设置绑定方案


         sched_setaffinity(0,sizeof(cpu_set_t), &mask);


}


该段代码将paralle
region里面的8个线程依次绑定到核2,5,13,9,3,6,7,4。同样可以使用sched_getaffinity函数获取线程的能够使用的核的列表,示例代码如下:


int num_processors = sysconf(_SC_NPROCESSORS_CONF); //获取当前节点核的数目


cpu_set_t get;


int i = 0;


CPU_ZERO(&get);


sched_getaffinity(0, sizeof(cpu_set_t), &get); //获取当前调用线程的可以使用的核


for(i = 0; i < num_processors; i++)


{


         if(CPU_ISSET(i, &get))


         {


                   printf(“The current thread %d bound to core %d\n“,
omp_get_thread_num(), i);


         }


}


下面是一个完整的例子


文件bind.c


#include<stdlib.h>


#include<stdio.h>


#include<sys/types.h>


#include<sys/sysinfo.h>


#include<unistd.h>


 


#define __USE_GNU


#include<sched.h>


#include<ctype.h>


#include<string.h>


#include<pthread.h>


#define THREAD_MAX_NUM 100  //1个CPU内的最多进程数


 


int num=0;  //cpu中核数


void* threadFun(void* arg)  //arg  传递线程标号(自己定义)


{


         cpu_set_t mask;  //CPU核的集合


         cpu_set_t get;   //获取在集合中的CPU


         int *a = (int *)arg; 


         printf("the a is:%d\n",*a);  //显示是第几个线程


         CPU_ZERO(&mask);    //置空


         CPU_SET(*a,&mask);   //设置亲和力值


         if (sched_setaffinity(0, sizeof(mask), &mask) ==
-1)//设置线程CPU亲和力


         {


                   printf("warning: could not set CPU affinity,
continuing...\n");


         }


         while (1)


         {


                   CPU_ZERO(&get);


                   if (sched_getaffinity(0, sizeof(get), &get) ==
-1)//获取线程CPU亲和力


                   {


                            printf("warning: cound not get thread affinity,
continuing...\n");


                   }


                   int i;


                   for (i = 0; i < num; i++)


                   {


                            if (CPU_ISSET(i, &get))//判断线程与哪个CPU有亲和力


                            {


                                     printf("this thread %d is running
processor : %d\n", i,i);


                            }


                   }


         }


 


         return NULL;


}


 


int main(int argc, char* argv[])


{


         num = sysconf(_SC_NPROCESSORS_CONF);  //获取核数


         pthread_t thread[THREAD_MAX_NUM];


         printf("system has %i processor(s). \n", num);


         int tid[THREAD_MAX_NUM];


         int i;


         for(i=0;i<num;i++)


         {


                   tid[i] = i;  //每个线程必须有个tid[i]


                  
pthread_create(&thread[0],NULL,threadFun,(void*)&tid[i]);


         }


         for(i=0; i< num; i++)


         {


                  
pthread_join(thread[i],NULL);//等待所有的线程结束,线程为死循环所以CTRL+C结束


         }


         return 0;


}


 


编译命令:gcc bind.c -o bind -lpthread


执行:./bind


输出结果:略


 


特别注意:


#define __USE_GNU不要写成#define _USE_GNU


#include<pthread.h>必须写在#define __USE_GNU之后,否则编译会报错


查看你的线程情况可以在执行时在另一个窗口使用top -H来查看线程的情况,查看各个核上的情况请使用top命令然后按数字“1”来查看。

你可能感兴趣的:(多核多线程编程,内核的绑定)