为线程绑定CPU

// learn gcc atomic variable
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <linux/unistd.h>
#include <errno.h>
#include <sched.h>

#define INC_TO 1000000 // every thread adds 1 million times

int global_int = 0;

// get current thread id
pid_t gettid(void){
    return syscall(__NR_gettid);
    //return pthread_self(); // cannot work...
}

void *thread_routine(void *arg){
    int i;
    int proc_num = (int)(long)(arg);
    cpu_set_t set; // A CPU affinity mask

    CPU_ZERO(&set);
    CPU_SET(proc_num, &set);

    // ets the CPU affinity mask of this thread to the value specified by maski.
    //A thread's CPU affinity mask determines the set of CPUs on which it is eligible to run.
    if(sched_setaffinity(gettid(), sizeof(cpu_set_t), &set)){
        perror("sched_setaffinity");
        return NULL;
    }

    for(i = 0; i < INC_TO; i++){
        //global_int ++ ;
        __sync_fetch_and_add(&global_int, 1);
    }

    return NULL;
}

int main(){
    int procs = 0;
    int i;
    pthread_t *threads;

    //get the number of processors currently online (available).
    procs = (int)sysconf(_SC_NPROCESSORS_ONLN);   // mine is 16
    if(procs < 0){
        perror("sysconf");
        return -1;
    }

    threads = malloc(sizeof(pthread_t) * procs);
    if(threads == NULL){
        perror("malloc threads");
        return -1;
    }

    printf("Set up %d threads ....\n", procs);
    for( i = 0; i < procs ; i++){
        if(pthread_create(&threads[i], NULL, thread_routine, (void *)(long)i)){
            perror("pthread_create");
            procs = i;
            break;
        }
    }

    for( i = 0; i < procs; i++){
        pthread_join(threads[i], NULL);
    }

    free(threads);

    printf("All threads work done.global_int is %d\n", global_int);
    printf("The expected value is %d\n" , INC_TO * procs);

    return 0;
}

参考:http://www.alexonlinux.com/multithreaded-simple-data-type-access-and-atomic-variables

时间: 2024-08-25 22:53:28

为线程绑定CPU的相关文章

Linux编程之《进程/线程绑定CPU》

Intro----- 通常我们在编写服务器代码时,可以通过将当前进程绑定到固定的CPU核心或者线程绑定到固定的CPU核心来提高系统调度程序的效率来提高程序执行的效率,下面将完整代码贴上.```/************************************************ * 该例程讲解了进程.线程绑定到固定的cpu核心上运行 * 来提高程序运行效率************************************************/#include <unistd

Windows10 临时将线程绑定至指定CPU的方法

本文首发:https://www.somata.work/2019/WindowsThreadBind.html 将线程绑定至指定CPU,这个应该时很多管理员需要了解认知的操作了吧,这样可以在一定程度上加快那么一丢丢程序运行速度,同时最重要的就是可以通过限制程序的运行CPU范围来保证至少有1颗CPU不会满状态运行,不至于卡到宕机,从而不影响后期维护. 那么这里将会介绍如何在Windows中临时设置绑定CPU的方法,下面来看操作即可 通过任务管理器绑定CPU 首先右键 Windows 菜单,选择任

NGINX源码剖析 之 CPU绑定(CPU亲和性)

作者:邹祁峰 邮箱:[email protected] 博客:http://blog.csdn.net/qifengzou 日期:2014.06.12 18:44 转载请注明来自"祁峰"的CSDN博客 1 引言   非统一内存访问(NUMA)是一种用于多处理器的电脑记忆体设计,内存访问时间取决于处理器的内存位置. 在NUMA下,处理器访问它自己的本地存储器的速度比非本地存储器(存储器的地方到另一个处理器之间共享的处理器或存储器)快一些. 针对NUMA架构系统的特点,可以通过将进程/线程

Linux进程或线程绑定到CPU

Linux进程或线程绑定到CPU 为了让程序拥有更好的性能,有时候需要将进程或线程绑定到特定的CPU,这样可以减少调度的开销和保护关键进程或线程. 进程绑定到CPU Linux提供一个接口,可以将进程绑定到特定的CPU: #include <sched.h> int sched_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask); int sched_getaffinity(pid_t pid, size_t cpu

linux下将不同线程绑定到不同core和cpu上——pthread_setaffinity_np

=============================================================== linux下的单进程多线程的程序,要实现每个线程平均分配到多核cpu,主要有2个方法 1:利用linux系统自己的线程切换机制,linux有一个服务叫做irqbalance,这个服务是linux系统自带的,默认会启动,这个服务的作用就是把多线程平均分配到CPU的每个核上面,只要这个服务不停止,多线程分配就可以自己实现.但是要注意,如果线程函数内部的有某个循环,且该循环内

linux 线程与CPU绑定

看到很多程序都是根据CPU个数来创建线程个数,当时很不理解他们之间的关系,咨询了项目组的同事后才有了大致了解.  1. 相关系统函数 下面的函数可以通过man命令查询到. SYNOPSIS #define _GNU_SOURCE #include <pthread.h> int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize, const cpu_set_t *cpuset); int pthread_getaffinity

linux 将进程或者线程绑定到指定的cpu上

基本概念 cpu亲和性(affinity) CPU的亲和性, 就是进程要在指定的 CPU 上尽量长时间地运行而不被迁移到其他处理器,也称为CPU关联性:再简单的点的描述就将指定的进程或线程绑定到相应的cpu上:在多核运行的机器上,每个CPU本身自己会有缓存,缓存着进程使用的信息,而进程可能会被OS调度到其他CPU上,如此,CPU cache命中率就低了,当绑定CPU后,程序就会一直在指定的cpu跑,不会由操作系统调度到其他CPU上,性能有一定的提高. 软亲和性(affinity) 就是进程要在指

Ubuntu系统进程绑定CPU核

Ubuntu系统进程绑定CPU核 作者:chszs,版权所有,未经同意,不得转载.博主主页:http://blog.csdn.net/chszs 本文讲述如何在Ubuntu系统中,把指定的进程绑定到指定的CPU核运行.而通常是由操作系统负责管理进程和线程的调度,但是这种情况下是不清楚由哪个CPU核运行你的进程,因为操作系统的调度是基于资源的可用性进行判断的. 可以这样,把指定的CPU核绑定到你的进程. taskset -cp <CPU ID | CPU IDs> <Process ID&

[原创]java WEB学习笔记94:Hibernate学习之路---session 的管理,Session 对象的生命周期与本地线程绑定

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 -----------------------------------------------------------------------------------------------------------------