- 論壇徽章:
- 0
|
問題解決了,linux下用sched_getcpu即可,不過要求內(nèi)核高于2.6.19,glibc高于2.6。另外,cpu的親和性只是說可以把某個線程綁定到某個cpu上,而不能獲取當前的線程正被綁定到某個cpu上。
http://www.kernel.org/doc/man-pa ... sched_getcpu.3.html- #include<stdlib.h>
- #include<stdio.h>
- #include<sys/types.h>
- #include<sys/sysinfo.h>
- #include<unistd.h>
- #define _GNU_SOURCE
- #include <sched.h>
- #include<ctype.h>
- #include<string.h>
- #include<pthread.h>
- void get_process() {
- printf("cpu is %d\n",sched_getcpu());
- }
- void *test(void *arg) {
- long long i;
- get_process();
- return (void*)0;
- }
- int main(int argc, char* argv[])
- {
- pthread_t tid;
- pthread_create(&tid,NULL,test,NULL);
- pthread_create(&tid,NULL,test,NULL);
- pthread_create(&tid,NULL,test,NULL);
- pthread_create(&tid,NULL,test,NULL);
- pthread_create(&tid,NULL,test,NULL);
- get_process();
- return 0;
- }
復(fù)制代碼 |
|