// 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体验高扩展艺术般的适配器

    前言 本篇文章带大家体验一下一种具有扩展性的适配器写法. 这个适配器主要用于Item有多种的情况下.当然仅仅有一种类型也是适用的 实现 毫无疑问我们要继承BaseAdapter,重写getCount, ...

  2. 部署zookeeper实践

    1.解压zookeeper 2.环境变量设置 hadoop@namenode:~/zookeeper-3.4.6/conf$ sudo vim /etc/profile export JAVA_HOM ...

  3. HDOJ 5296 Annoying problem LCA+数据结构

    dfs一遍得到每一个节点的dfs序,对于要插入的节点x分两种情况考虑: 1,假设x能够在集合中的某些点之间,找到左边和右边距离x近期的两个点,即DFS序小于x的DFS序最大点,和大于x的DFS序最小的 ...

  4. 集群环境下,Session管理的几种手段

    集群环境下,Session管理的几种手段 1.Session复制 缺点:集群服务器间需要大量的通信进行Session复制,占用服务器和网络的大量资源. 由于所有用户的Session信息在每台服务器上都 ...

  5. js中如何获取对象的长度和名称

    js如何获取对象长度和名称 一.总结 一句话总结:对象的长度不能用.length获取,用js原生的Object.keys可以获取到 var obj = {'name' : 'Tom' , 'sex' ...

  6. 了解Linux的基础知识和一般概念

    1.GNU和GPL    GNU计划(又称革奴计划),是由Richard Stallman(理查德·斯托曼)在1983年9月27日公开发起的自由软件集体协作计划.它的目标是创建一套完全自由的操作系统. ...

  7. iOS开发——NSString小结

    1.创建常量字符串. NSString *astring = @"This is a String!";   2.创建空字符串,给予赋值. NSString *astring = ...

  8. Java 调用对象方法的执行过程

    弄清调用对象方法的执行过程十分重要.下面是调用过程的详细描述: 1) 编译器查看对象的声明类型和方法名.假设调用x.f(param),且隐式参数x声明为C类的对象.需要注意的是:有可能存在多个名为f, ...

  9. 今日SGU 6.5

    sgu 160 题意:给你n个数字 数字范围 1 到 m 问你从中取出任意数量的数字使得这些数字的积取模m最大 收获:dp,记录dp的路径 #include<bits/stdc++.h> ...

  10. 查看mysql正在执行的SQL语句,使用profile分析SQL执行状态

    http://qq85609655.iteye.com/blog/2113960 1)我们先通过status命令查看Mysql运行状态 mysql> status; -------------- ...