![](/img/trans.png)
[英]Is it possible to set affinity with sched_setaffinity in Android?
[英]error: undefined reference to `sched_setaffinity' on windows xp
基本上,下面的代码旨在在Linux上使用,也许这就是我使用Windows XP时出现错误的原因,但我认为pthreads在两台计算机上都应该工作得很好。 我使用gcc作为编译器,并且确实与-lpthread链接,但无论如何我都遇到了以下错误。
| 21 |对sched_setaffinity'| |30|undefined reference to
未定义引用sched_setaffinity'| |30|undefined reference to
sched_setaffinity'| |30|undefined reference to
sched_setaffinity的sched_setaffinity'| |30|undefined reference to
'|
如果还有另一种使用pthreads设置线程相似性的方法(在Windows上),请告诉我。 我已经了解了所有可用的windows.h线程相似性函数,但是我想使事情保持多平台。 谢谢。
#include <stdio.h>
#include <math.h>
#include <sched.h>
double waste_time(long n)
{
double res = 0;
long i = 0;
while(i <n * 200000)
{
i++;
res += sqrt (i);
}
return res;
}
int main(int argc, char **argv)
{
unsigned long mask = 1; /* processor 0 */
/* bind process to processor 0 */
if (sched_setaffinity(0, sizeof(mask), &mask) <0)//line 21
{
perror("sched_setaffinity");
}
/* waste some time so the work is visible with "top" */
printf ("result: %f\n", waste_time (2000));
mask = 2; /* process switches to processor 1 now */
if (sched_setaffinity(0, sizeof(mask), &mask) <0)//line 30
{
perror("sched_setaffinity");
}
/* waste some more time to see the processor switch */
printf ("result: %f\n", waste_time (2000));
}
sched_getaffinity()
和sched_setaffinity()
严格来说是特定于Linux的调用。 Windows提供了自己的一组特定的Win32 API调用,这些调用会影响计划。 有关Windows的示例代码,请参见此答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.