为线程绑定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关联性:再简单的点的描述就将指定的进程或线程绑定到相应的 ...
随机推荐
- JNI之——'cl' 不是内部或外部命令,也不是可执行的程序或批处理文件
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46604315 问题的出现: 今天卸载了VS2010,重装vs2008后.发 ...
- java学习记录笔记--继承,super,Object类
继承: Java中的继承是单继承的. 1.子类拥有父类的全部属性和方法. 可是属性和方法的修饰符不能使private. 2.能够复用父类的代码. 方法的重写须要满足的条件: a.返回值类型 b.方法名 ...
- Android组件系列----ContentProvider内容提供者【1】
[正文] 一.ContentProvider简单介绍: ContentProvider内容提供者(四大组件之中的一个)主要用于在不同的应用程序之间实现数据共享的功能. ContentProvider能 ...
- 编写SDR SDRAM页突发模式控制器的注意点-下篇
本来是没打算写这些的,但是后面逐渐发现点问题,所以决定再写一个下篇来补充说明一下. 图一 细心的网友会发现上篇末尾的打印是有点问题的,因为我的数据产生器产生的是1-200,1-200,1-200,1- ...
- Android Gallery和ImageSwitcher同步自动(滚动)播放图片库
本文主要内容是如何让Gallery和ImageSwitcher控件能够同步自动播放图片集 ,看起来较难,然而,实现的方法非常简单, 请跟我慢慢来.总的来说,本文要实现的效果如下图:(截图效果不怎么好) ...
- UVa 1599 Ideal Path【BFS】
题意:给出n个点,m条边,每条边上涂有一个颜色,求从节点1到节点n的最短路径,如果最短路径有多条,要求经过的边上的颜色的字典序最小 紫书的思路:第一次从终点bfs,求出各个节点到终点的最短距离, 第二 ...
- 链表python
无序链表.有序链表 有序列表排序通常是升序或降序,并且我们假设列表项具有已经定义的有意义的比较运算. 许多有序列表操作与无序列表的操作相同. 必须明确链表的第一项位置,一旦知道第一项. 链表实现的基本 ...
- vue下载模板、导出excle
1.下载模板是 下载模版比较简单,就是跳一个新的页面 2.导出excle 就是get请求,把自己要导出的参数一一拼接起来 }
- linux上将另一个文件内容快速写入正在编辑的文件内
一.我们看到 www 目录下有两个文件.like.php 内有一行字母,而 loo.php 内什么也没有. 二 .我们来编辑 loo.php. 三.用下面的命令将 like.php 的内容复制到 lo ...
- Git 内部原理 - (3) Git 引用 (4)包文件
Git 引用 我们可以借助类似于 git log 1a410e 这样的命令来浏览完整的提交历史,但为了能遍历那段历史从而找到所有相关对象,你仍须记住 1a410e 是最后一个提交. 我们需要一个文件来 ...