// 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

为线程绑定CPU的更多相关文章

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

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

  2. 线程绑定CPU核-sched_setaffinity

    CPU亲合力就是指在Linux系统中能够将一个或多个进程绑定到一个或多个处理器上运行. 一个进程的CPU亲合力掩码决定了该进程将在哪个或哪几个CPU上运行.在一个多处理器系统中,设置CPU亲合力的掩码 ...

  3. linux线程绑定cpu

    函数介绍 #define __USE_GNU #include <sched.h> void CPU_ZERO(cpu_set_t *set); void CPU_SET(int cpu, ...

  4. 线程绑定cpu

    #include <stdio.h> #include <pthread.h> #include <sys/sysinfo.h> #include <unis ...

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

    本文首发:https://www.somata.work/2019/WindowsThreadBind.html 将线程绑定至指定CPU,这个应该时很多管理员需要了解认知的操作了吧,这样可以在一定程度 ...

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

    作者:邹祁峰 邮箱:Qifeng.zou.job@gmail.com 博客:http://blog.csdn.net/qifengzou 日期:2014.06.12 18:44 转载请注明来自&quo ...

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

    =============================================================== linux下的单进程多线程的程序,要实现每个线程平均分配到多核cpu,主 ...

  8. Linux进程或线程绑定到CPU

    Linux进程或线程绑定到CPU 为了让程序拥有更好的性能,有时候需要将进程或线程绑定到特定的CPU,这样可以减少调度的开销和保护关键进程或线程. 进程绑定到CPU Linux提供一个接口,可以将进程 ...

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

    基本概念 cpu亲和性(affinity) CPU的亲和性, 就是进程要在指定的 CPU 上尽量长时间地运行而不被迁移到其他处理器,也称为CPU关联性:再简单的点的描述就将指定的进程或线程绑定到相应的 ...

随机推荐

  1. Android 进程常驻(0)----MarsDaemon使用说明

    版权声明:本文为博主原创文章,未经博主允许不得转载. 这是一个轻量级的库,配置几行代码,就可以实现在Android上实现进程常驻,也就是在系统强杀下,以及360获取root权限下,clean mast ...

  2. 安卓开发,adb shell 调试sqlite3数据库

    安卓开发,adb shell 调试sqlite3数据库 在安卓中创建了sqlite3数据库,想要调试怎么办? 通过adb shell来进行查看. 第一步,将adb加入到系统变量中. 这样就可以在命令行 ...

  3. hpuoj--校赛--考试来了(水题)

    问题 C: 感恩节KK专场--考试来了 时间限制: 1 Sec  内存限制: 128 MB 提交: 475  解决: 112 [提交][状态][讨论版] 题目描述 很多课程马上就结课了,随之而来的就是 ...

  4. 6. Intellij Idea 2017创建web项目及tomcat部署实战

    转自:https://www.cnblogs.com/shindo/p/7272646.html 相关软件:Intellij Idea2017.jdk16.tomcat7 Intellij Idea直 ...

  5. Hadoop框架基础(一)

    ** Hadoop框架基础(一)     学习一个新的东西,传统而言呢,总喜欢漫无目的的扯来扯去,比如扯扯发展史,扯扯作者是谁,而我认为这些东西对于刚开始接触,并以开发为目的学者是没有什么帮助的,反而 ...

  6. Java类和对象11

    首先,编写一个类ChongZai,该类中有3个重载的方法void print():其次,再编写一个主类来测试ChongZai类的功能. public class ChongZai { public v ...

  7. <Sicily>数字反转

    一.题目描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2). 二.输入 输入共 1 行, ...

  8. Python3基础笔记---re模块

    参考博客: Py西游攻关之模块 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列 ...

  9. 手把手教你进行R语言的安装及安装过程中相关问题解决方案

    这篇文章旨在为R语言的新手铺砖引路,行文相对基础,希望对在R语言安装上有问题的小伙伴提供帮助和指引.一.什么是 R 语言R 编程语言被广泛应用在统计科学和商业领域. 在各种编程语言排名中 R 语言的排 ...

  10. SSD-tensorflow-2 evaluation

    测试就是用voc2007的test set来测试已经训练好的checkpoint的mAP,github上提供了三个已经训练好的model的checkpoint checkpoint 里面已有的300_ ...