- 论坛徽章:
- 0
|
问题解决了,linux下用sched_getcpu即可,不过要求内核高于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;
- }
复制代码 |
|