为线程绑定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
为线程绑定CPU的更多相关文章
- Linux编程之《进程/线程绑定CPU》
Intro----- 通常我们在编写服务器代码时,可以通过将当前进程绑定到固定的CPU核心或者线程绑定到固定的CPU核心来提高系统调度程序的效率来提高程序执行的效率,下面将完整代码贴上. /***** ...
- 线程绑定CPU核-sched_setaffinity
CPU亲合力就是指在Linux系统中能够将一个或多个进程绑定到一个或多个处理器上运行. 一个进程的CPU亲合力掩码决定了该进程将在哪个或哪几个CPU上运行.在一个多处理器系统中,设置CPU亲合力的掩码 ...
- linux线程绑定cpu
函数介绍 #define __USE_GNU #include <sched.h> void CPU_ZERO(cpu_set_t *set); void CPU_SET(int cpu, ...
- 线程绑定cpu
#include <stdio.h> #include <pthread.h> #include <sys/sysinfo.h> #include <unis ...
- Windows10 临时将线程绑定至指定CPU的方法
本文首发:https://www.somata.work/2019/WindowsThreadBind.html 将线程绑定至指定CPU,这个应该时很多管理员需要了解认知的操作了吧,这样可以在一定程度 ...
- NGINX源代码剖析 之 CPU绑定(CPU亲和性)
作者:邹祁峰 邮箱:Qifeng.zou.job@gmail.com 博客:http://blog.csdn.net/qifengzou 日期:2014.06.12 18:44 转载请注明来自&quo ...
- linux下将不同线程绑定到不同core和cpu上——pthread_setaffinity_np
=============================================================== linux下的单进程多线程的程序,要实现每个线程平均分配到多核cpu,主 ...
- Linux进程或线程绑定到CPU
Linux进程或线程绑定到CPU 为了让程序拥有更好的性能,有时候需要将进程或线程绑定到特定的CPU,这样可以减少调度的开销和保护关键进程或线程. 进程绑定到CPU Linux提供一个接口,可以将进程 ...
- linux 将进程或者线程绑定到指定的cpu上
基本概念 cpu亲和性(affinity) CPU的亲和性, 就是进程要在指定的 CPU 上尽量长时间地运行而不被迁移到其他处理器,也称为CPU关联性:再简单的点的描述就将指定的进程或线程绑定到相应的 ...
随机推荐
- bzoj1005: [HNOI2008]明明的烦恼(prufer+高精度)
1005: [HNOI2008]明明的烦恼 题目:传送门 题解: 毒瘤题啊天~ 其实思考的过程还是比较简单的... 首先当然还是要了解好prufer序列的基本性质啦 那么和1211大体一致,主要还是利 ...
- What are the differences between WebAPI and WebAPI 2
http://stackoverflow.com/questions/21298961/what-are-the-differences-between-webapi-and-webapi-2 Maj ...
- 11.IntelliJ IDEA详细配置和使用教程(适用于Java开发人员)
转自:https://blog.csdn.net/chssheng2007/article/details/79638076 前言 正所谓工欲善其事必先利其器,对开发人员而言若想提高编码效率,一款高效 ...
- IT男送什么礼物给女朋友呢?
每到各种节日,加班到吐血的IT男们,总是没什么时间准备礼物给女朋友,偶尔想起又不知道送什么的好,女朋友虽然心里不说,多少有些难过.现在推荐朋友的羊毛毡手工店,每一件都是亲手制作,特别是可以个性定制,女 ...
- <Sicily>Pair
一.题目描述 The N cities of Estiah are connected by N-1 roads. The roads are built in a way that it's alw ...
- ReactiveCocoa使用记录-网络登录事件
对于一个应用来说,绝大部分的时间都是在等待某些事件的发生或响应某些状态的变化,比如用户的触摸事件.应用进入后台.网络请求成功刷新界面等等,而维护这些状态的变化,常常会使代码变得非常复杂,难以扩展.而 ...
- c#做对比软件
一些 HTML内容比较/文本差异比较 开源代码 1. DiffPlex - a .NET Diff Generator http://diffplex.codeplex.com/SourceCo ...
- centos 安装 aria2 webui 实现网页下载
centos aria2 webui 安装aria2 安装 rpmforge源 wget http://repository.it4i.cz/mirrors/repoforge/redhat/el6/ ...
- windows或linux安装python
一.windows安装 先进入 python 官网:https://www.python.org/downloads/windows/ 选择合适的版本下载: 下载完成,双击运行安装[勾选Add to ...
- [USACO16FEB]围栏Fenced In Platinum
题目:洛谷P3141. 题目大意:有一个方形区域,被分成若干区域.现在要去掉若干条围栏,使得所有区域连通,求最少去掉多少长度的围栏. 解题思路:贪心.建议画图思考. 先对围栏位置进行排序,然后相邻两条 ...