参考了这篇文章:https://blog.csdn.net/lanyzh0909/article/details/50404664

大体的代码如下:

#include <pthread.h>
#include <sched.h> #include <stdio.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <unistd.h> void* _thread_func(void* param){
int thread_index = *(int*)param;
int cpu_count = sysconf(_SC_NPROCESSORS_CONF);
cpu_set_t mask; // CPU核的集合
cpu_set_t get; //获取在集合中的CPU
CPU_ZERO(&mask); //置空
CPU_SET(thread_index, &mask); //设置亲和力值
//设置线程CPU亲和力
if (sched_setaffinity(0, sizeof(mask), &mask) == -1) {
printf("warning: could not set CPU %d affinity, continuing...\n", thread_index);
} else {
printf("thread %d set to cpu %d\n", thread_index, thread_index);
}
}

在高通骁龙835处理器上,始终打印如下内容:

warning: could not set CPU 7 affinity, continuing...

thread 1 set to cpu 1

thread 0 set to cpu 0

thread 3 set to cpu 3

thread 2 set to cpu 2

warning: could not set CPU 4 affinity, continuing...

thread 5 set to cpu 5

thread 6 set to cpu 6

猜测是系统故意让其中的两个核无法被设置亲缘性,原因未知。

【记录一个问题】android ndk下设置线程的亲缘性,总有两个核无法设置成功的更多相关文章

  1. 【记录一个问题】ndk下使用c++11的condition_variable问题较多

    1.存在通知丢失的情况:生产者线程通知196次,消费者线程收到190次,导致部分数据无法被处理. 2.cond.wait()方法后的加锁有问题,导致对空队列进行出队操作然后coredump.一直记得w ...

  2. CPU affinity 进程和线程的亲缘性

    设置Processor Affinity 作用: 1.进程和线程的亲缘性(affinity),使进程或线程在指定的CPU(核)上运行.(比如程序A,在第4个核心上运行) 2.设置进程 或者 线程, 使 ...

  3. Android NDK 下的宽字符编码转换及icu库的使用(转)

    原贴http://topic.csdn.net/u/20101022/16/1b2e0cec-b9d2-42ea-8d9c-4f1bb8320a54.html?r=70149216 ,看过并动手实现, ...

  4. Android ndk下用AssetManager读取assets的资源

    转自:http://www.cppblog.com/johndragon/archive/2012/12/28/196754.html 在使用 cocos2dx 在 Android 上进行游戏开发时, ...

  5. 【记录一个问题】macos下lldb调试opencv的一个程序,出现“failed to load objfile for”错误,并且无法调试进入opencv的函数

    opencv编译使用了Debug版本,打开了BUILD_WITH_DEBUG_INFO=ON选项. 发现问题后,我又在CMAKE_CXX_FLAGS_DEBUG中设置为 -g -ggdb3,在CMAK ...

  6. android ndk下没有pthread_yield,好在std::this_thread::yield()可以达到同样的效果

    一个多线程的算法中,发现线程利用率只有47%左右,大量的处理时间因为usleep(500)而导致线程睡眠: 性能始终上不去. 把usleep(500)修改为std::this_thread::yiel ...

  7. 【记录一个问题】macos下使用opencl, clSetEventCallback不生效

    一开始的调用顺序是这样: enqueueWriteBuffer enqueueNDRangeKernel enqueueReadBuffer SetEventCallback 执行后主程序用getch ...

  8. 【记录一个问题】linux下使用opencv中的UMat,性能并未提升,反而略有下降

    使用后性能略微下降,一开始怀疑是UMat拷贝的问题.运行 nvidia-smi -l 1, 发现GPU占用始终为0.说明opencl使用的是CPU版本,而不是GPU版本.明天试验opencl的GPU版 ...

  9. 第8章 用户模式下的线程同步(4)_条件变量(Condition Variable)

    8.6 条件变量(Condition Variables)——可利用临界区或SRWLock锁来实现 8.6.1 条件变量的使用 (1)条件变量机制就是为了简化 “生产者-消费者”问题而设计的一种线程同 ...

随机推荐

  1. ffmpeg 系列博客

    https://www.ffmpeg.org/download.html#build-macffmpeg 系列博文https://me.csdn.net/blog/leixiaohua1020http ...

  2. java 数据类型:<泛型>在方法中和在构造器中的应用

    背景: Java不允许我们把对象放在一个未知的集合中. import java.util.ArrayList; import java.util.List; /** * @ClassName Meth ...

  3. Raft成员变化(Membership Change)

    我司高产作家唐刘老师的小猪佩奇版"深入浅出 Raft"第四弹来啦~~前几篇内容戳这里 ↓ 在猪爸爸的努力下,泥坑银行终于能高效正常的运作了,但猪爸爸一直比较担心海盗岛那边的网点,因 ...

  4. SpringBoot项目使用Caffeine本地缓存

    环境配置:(或以上版本,必须) JDK 版本:1.8  Caffeine 版本:2.8.0SpringBoot 版本:2.2.2.RELEASE 也可以不与SpringBoot结合 1.添加maven ...

  5. 【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...

  7. 【LeetCode】50. Pow(x, n) 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 递归 迭代 日期 题目地址: https://le ...

  8. 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. Codeforces 919D:Substring(拓扑排序+DP)

    D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...

  10. Improving Variational Auto-Encoders using Householder Flow

    目录 概 主要内容 代码 Tomczak J. and Welling M. Improving Variational Auto-Encoders using Householder Flow. N ...