• Linux CPU线程绑核


     

            为了加快程序的运行速度和充分利用CPU资源,我们可以人为将不同线程绑定在不同的cup上,例如有两个线程A,B,其中A已经在CPU0上运行,并且CPU0上还有其他的任务,那么我们可以将线程B绑到CPU1上,这样就可以减轻CPU0的负担,从而充分利用多核CPU。原来是一个CPU做两件事,现在两个CPU同时做两个事,使效率更高。

    看下下面的代码

    1. #define _GNU_SOURCE
    2. #include //这两个头文件是一起的
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. void *myfun(void *arg)
    9. {
    10. cpu_set_t mask;
    11. cpu_set_t get;
    12. CPU_ZERO(&mask);
    13. CPU_SET(1, &mask);
    14. if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)
    15. {
    16. fprintf(stderr, "set thread affinity failed\n");
    17. }
    18. CPU_ZERO(&get);
    19. if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0)
    20. {
    21. fprintf(stderr, "get thread affinity failed\n");
    22. }
    23. if (CPU_ISSET(1, &get))
    24. {
    25. printf("thread %lu is running in processor 1\n", (long unsigned)pthread_self());
    26. }
    27. pthread_exit(NULL); //退出线程
    28. }
    29. int main(int argc, char *argv[])
    30. {
    31. pthread_t tid; //用来创建新的线程
    32. cpu_set_t mask; //用来设置
    33. cpu_set_t get; //用来获
    34. int num = sysconf(_SC_NPROCESSORS_CONF); //获取CPU核数
    35. printf("system has %d processor(s)\n", num);
    36. if (pthread_create(&tid, NULL, (void *)myfun, NULL) != 0) //创建线程 myfun为入口函数
    37. {
    38. fprintf(stderr, "thread create failed\n");
    39. return -1;
    40. }
    41. CPU_ZERO(&mask); //初始化某个CPU集,设置为空
    42. CPU_SET(0, &mask); //将某个CPU加入到这个CPU集合里,这里是,也可以理解为,绑定CPU,这里是CPU0
    43. if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) //pthread_self():获得线程自身ID
    44. { //设置某一线程运行在某个CPU上
    45. fprintf(stderr, "set thread affinity failed\n");
    46. }
    47. CPU_ZERO(&get);
    48. if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0)
    49. { //查看某一个CPU上有哪些线程
    50. fprintf(stderr, "get thread affinity failed\n");
    51. }
    52. if (CPU_ISSET(0, &get)) { //判断某个CPU是不是在CPU集里,这里为CPU0
    53. printf("thread %lu is running in processor 0\n", (long unsigned)pthread_self());
    54. }
    55. pthread_join(tid, NULL); //等待线程
    56. return 0;
    57. }

     编译一下

    gcc cpu.c -pthread

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

    可以看到我们将两个线程分别绑在了不同的CPU上面。

    这里总结一下用到的基本函数:

    1. void CPU_ZERO (cpu_set_t *set);   //初始化,设为空
    2. void CPU_SET (int cpu, cpu_set_t *set)//将某个cpu加入cpu集中 
    3. void CPU_CLR (int cpu, cpu_set_t *set)//将某个cpu从cpu集中移出 
    4. int CPU_ISSET (int cpu, const cpu_set_t *set)//判断某个cpu是否已在cpu集中设置了
    5. int pthread_setaffinity_np(pthread_t thread,size_t cpusetsize,const cpu_set_t *cpuset)//设置CPU
    6. int pthread_getaffinity_np(pthread_t thread,size_t cpusetsize, cpu_set_t *cpuset)//查看CPU

  • 相关阅读:
    答应我,在vue中不要滥用watch好吗?
    【GlobalMapper精品教程】004:生成标准经纬网图幅(1:100万)案例教程
    Python面向对象2-继承-
    (っ•̀ω•́)っ 如何在PPT中为文本框添加滚动条
    【C++】string类模拟实现
    使用命令部署Java项目的基本步骤
    深拷贝与浅拷贝
    Linux - 札记 - W10: Warning: Changing a readonly file
    八大排序详解
    生存资料ROC曲线的最佳截点和平滑曲线
  • 原文地址:https://blog.csdn.net/u013253075/article/details/132636185