作者wtchen (沒有存在感的人)
看板LinuxDev
標題Re: [問題] 請問是否可以強制多執行緒共用一個cpu
時間Sun Oct 23 18:09:05 2016
上來回報測試結果:
https://gist.github.com/gnitnaw/d3572c7e90e07fc1b76d25d540be7f3d
gcc thread.c -Wall -pedantic -lpthread -o thread
並沒有特意設繼承屬性,不過似乎預設就是繼承了
taskset的結果:
pid 4857's current affinity list: 0
(是在我的筆電上測試,64bit+ 4 core)
※ 引述《wtchen (沒有存在感的人)》之銘言:
: 正在構思一個程式,load其實不是很重,但是有3-4個thread必須輪流對單一device存取
: 但是想做到lock-free+節省load+空出其他core做其他的事,
: 所以希望把該程式生出的所有的thread都集中在單一個core跑。
: raspberry pi的case簡單(本來就是single core),可是想在rp3上也這樣做。
: 請問有在user space(pthread)實作的方法嗎?
: 我在 http://fred-zone.blogspot.fr/2007/10/cpu.html 看到一個例子:
: int main(void) {
: cpu_set_t cmask;
: unsigned long len = sizeof(cmask);
: __CPU_ZERO(&cmask); /* 初始化 cmask */
: __CPU_SET(0, &cmask); /* 指定第一個處理器 */
: /* 設定自己由指定的處理器執行 */
: if (!sched_setaffinity(0, len, &cmask)) {
: printf("Could not set cpu affinity for current process.\n");
: exit(1);
: }
: return 0;
: }
: 請問在sched_setaffinity(0, len, &cmask) 之後,不管該程式分出多少thread
: 都會只在cpu 0 執行嗎?
: 感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 86.209.148.33
※ 文章網址: https://www.ptt.cc/bbs/LinuxDev/M.1477217349.A.C8B.html
※ 編輯: wtchen (86.209.148.33), 10/23/2016 18:33:14
推 ooookkkk: 那如果要指定thread綁定cpu要如何實作 11/18 18:06
→ wtchen: 我附的程式碼有實作阿 11/18 20:42
→ wtchen: 不然可以用pthread_setaffinity_np 11/18 20:43
推 ooookkkk: 好的我試試看,感謝 11/18 22:07