Linux CPU threads tied to cores

 

        In order to speed up the running speed of the program and make full use of CPU resources, we can artificially bind different threads to different cups. For example, there are two threads A and B. Among them, A is already running on CPU0, and there are other threads on CPU0. task, then we can tie thread B to CPU1, which can reduce the burden on CPU0 and make full use of the multi-core CPU. Originally, one CPU did two things, but now two CPUs do two things at the same time, making it more efficient.

Take a look at the code below

#define _GNU_SOURCE
#include <sched.h>      //这两个头文件是一起的
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
 
void *myfun(void *arg)
{
    cpu_set_t mask;
    cpu_set_t get;
  
    CPU_ZERO(&mask);
    CPU_SET(1, &mask);
    if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) 
    {
        fprintf(stderr, "set thread affinity failed\n");
    }
 
    CPU_ZERO(&get);
 
    if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) 
    {
        fprintf(stderr, "get thread affinity failed\n");
    }
    if (CPU_ISSET(1, &get)) 
    {
            printf("thread %lu is running in processor 1\n", (long unsigned)pthread_self());
    }
    pthread_exit(NULL);     //退出线程
}
 
int main(int argc, char *argv[])
{
    pthread_t tid;          //用来创建新的线程
    cpu_set_t mask;         //用来设置
    cpu_set_t get;          //用来获
 
    int num = sysconf(_SC_NPROCESSORS_CONF);    //获取CPU核数
    printf("system has %d processor(s)\n", num);
 
    if (pthread_create(&tid, NULL, (void *)myfun, NULL) != 0)    //创建线程  myfun为入口函数
    {
        fprintf(stderr, "thread create failed\n");
        return -1;
    }
    
    CPU_ZERO(&mask);            //初始化某个CPU集,设置为空
    CPU_SET(0, &mask);          //将某个CPU加入到这个CPU集合里,这里是,也可以理解为,绑定CPU,这里是CPU0
    if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)       //pthread_self():获得线程自身ID
    {                                                                       //设置某一线程运行在某个CPU上
        fprintf(stderr, "set thread affinity failed\n");
    }
    
    CPU_ZERO(&get);
    
    if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) 
    {                                                                       //查看某一个CPU上有哪些线程
        fprintf(stderr, "get thread affinity failed\n");
    }
    if (CPU_ISSET(0, &get)) {                                               //判断某个CPU是不是在CPU集里,这里为CPU0
            printf("thread %lu is running in processor 0\n", (long unsigned)pthread_self());
    }
 
    pthread_join(tid, NULL);    //等待线程
    return 0;
}

 Compile it

gcc cpu.c -pthread

thread 139946778752832 is running in processor 0
thread 139946770462464 is running in processor 1

You can see that we have tied the two threads to different CPUs.

Here is a summary of the basic functions used:

void CPU_ZERO (cpu_set_t *set);   //初始化,设为空

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); //判断某个cpu是否已在cpu集中设置了

int pthread_setaffinity_np(pthread_t thread,size_t cpusetsize,const cpu_set_t *cpuset); //设置CPU

int pthread_getaffinity_np(pthread_t thread,size_t cpusetsize, cpu_set_t *cpuset); //查看CPU

Guess you like

Origin blog.csdn.net/u013253075/article/details/132636185