第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何設(shè)置特定pthread的CPU親和力?

如何設(shè)置特定pthread的CPU親和力?

我想指定特定pthread的cpu親和力。到目前為止,我發(fā)現(xiàn)的所有引用都涉及設(shè)置進(jìn)程(pid_t)而不是線程(pthread_t)的cpu親和力。我嘗試了一些傳遞pthread_t的實(shí)驗(yàn),并且按預(yù)期它們會失敗。我是否在嘗試做一些不可能的事情?如果沒有,您可以發(fā)送指針嗎?太感謝了。
查看完整描述

3 回答

?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個贊

這是我為了使生活更輕松而制作的包裝紙。它的作用是使調(diào)用線程被“塞住”到具有id的內(nèi)核core_id:


// core_id = 0, 1, ... n-1, where n is the system's number of cores


int stick_this_thread_to_core(int core_id) {

   int num_cores = sysconf(_SC_NPROCESSORS_ONLN);

   if (core_id < 0 || core_id >= num_cores)

      return EINVAL;


   cpu_set_t cpuset;

   CPU_ZERO(&cpuset);

   CPU_SET(core_id, &cpuset);


   pthread_t current_thread = pthread_self();    

   return pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset);

}


查看完整回答
反對 回復(fù) 2019-12-12
?
慕容3067478

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個贊

假設(shè)Linux:


設(shè)置相似性的界面是-您可能已經(jīng)發(fā)現(xiàn):


int sched_setaffinity(pid_t pid,size_t cpusetsize,cpu_set_t *mask);

傳遞0作為pid,它將僅適用于當(dāng)前線程,或者讓其他線程通過linux特定的調(diào)用報告其內(nèi)核pid pid_t gettid(void);并將其作為pid傳遞。


引用手冊頁


親和力掩碼實(shí)際上是每個線程的屬性,可以針對線程組中的每個線程分別進(jìn)行調(diào)整。調(diào)用gettid(2)返回的值可以在pid參數(shù)中傳遞。將pid指定為0將為調(diào)用線程設(shè)置屬性,并將從調(diào)用返回的值傳遞給getpid(2)將為線程組的主線程設(shè)置屬性。(如果使用的是POSIX線程API,請使用pthread_setaffinity_np(3)而不是sched_setaffinity()。)


查看完整回答
反對 回復(fù) 2019-12-12
?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個贊

//compilation: gcc -o affinity affinity.c -lpthread


#define _GNU_SOURCE

#include <sched.h>   //cpu_set_t , CPU_SET

#include <pthread.h> //pthread_t

#include <stdio.h>


void *th_func(void * arg); 


int main(void) {

  pthread_t thread; //the thread


  pthread_create(&thread,NULL,th_func,NULL); 


  pthread_join(thread,NULL);   


  return 0;

}



void *th_func(void * arg)

{  

  //we can set one or more bits here, each one representing a single CPU

  cpu_set_t cpuset; 


  //the CPU we whant to use

  int cpu = 2;


  CPU_ZERO(&cpuset);       //clears the cpuset

  CPU_SET( cpu , &cpuset); //set CPU 2 on cpuset



  /*

   * cpu affinity for the calling thread 

   * first parameter is the pid, 0 = calling thread

   * second parameter is the size of your cpuset

   * third param is the cpuset in which your thread will be

   * placed. Each bit represents a CPU

   */

  sched_setaffinity(0, sizeof(cpuset), &cpuset);


  while (1);

       ; //burns the CPU 2


  return 0;

}

在POSIX環(huán)境中,可以使用cpusets來控制進(jìn)程或pthread可以使用哪些CPU。這種類型的控制稱為CPU關(guān)聯(lián)。


函數(shù)“ sched_setaffinity”接收pthread ID和cpuset作為參數(shù)。當(dāng)您在第一個參數(shù)中使用0時,調(diào)用線程將受到影響


查看完整回答
反對 回復(fù) 2019-12-12
  • 3 回答
  • 0 關(guān)注
  • 990 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號