// 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. Getting Started with MongoDB (MongoDB Shell Edition)

    https://docs.mongodb.com/getting-started/shell/ Overview Welcome to the Getting Started with MongoDB ...

  2. visual studio code(vscode)的使用(快捷键)

    Visual Studio Code初探 vscode 是一种可运行于 OS X,Windows 和 Linux 之上的免费跨平台编辑器: 1. 快捷键 ctrl + `:调出(对于 windows ...

  3. BZOJ 1024 SCOI2009 生日快乐 暴搜

    思路:eng...按照题意搜就好了 (一定要注意题面的n<=10--) 枚举断点...反正n<=10不怂 //By SiriusRen #include <cstdio> #i ...

  4. 网络流Dinic算法模板 POJ1273

    这就是以后我的板子啦~~~ #include <queue> #include <cstdio> #include <cstring> #include <a ...

  5. leetcode 新题型----SQL,shell,system design

    leetcode 主要是一个针对北美的coder人群找工作的代码练习网站,我在2015年初次接触这个网站的时候,总共只有200多道题目,是一个类似acm 的a题网站.这些年变化越来越大,主要是因为找工 ...

  6. idea报错。Error:Failed to load project configuration: cannot parse xml file E:\project\.idea\workspace.xml: Error on line 1: 前言中不允许有内容。

    因为电脑卡死强制重启电脑后打开idea,进行junit单元测试报错: idea报错.Error:Failed to load project configuration: cannot parse x ...

  7. OnClientClick知识+一个上传的例子

    文件名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp ...

  8. OpenGL编程(五)绘直线以及分析绘直线的算法

    这次主要实现在窗口上绘制点.线以及修改其属性,另外还会分析画直线的原理和相关算法. 1.在窗口指定位置画点 glBegin(GL_POINTS); glEnd(); 使用glBegin()和glEnd ...

  9. python -迭代器与生成器 以及 iterable(可迭代对象)、yield语句

    我刚开始学习编程没多久,对于很多知识还完全不知道,而有些知道的也是一知半解,我想把学习到的知识记录下来,一是弥补记忆力差的毛病,二也是为了待以后知识能进一步理解透彻时再回来做一个补充. 参考链接: 完 ...

  10. 紫书 例题 10-20 UVa 10900(连续概率)

    分两类,当前第i题答或不答 如果不回答的话最大期望奖金为2的i次方 如果回答的话等于p* 下一道题的最大期望奖金 那么显然我们要取最大值 所以就要分类讨论 我们设答对i题后的最大期望奖金为d[i] 显 ...