简体   繁体   English

错误:在Windows XP上对`sched_setaffinity'的未定义引用

[英]error: undefined reference to `sched_setaffinity' on windows xp

Basically the code below was intended for use on linux and maybe thats the reason I get the error because I'm using windows XP, but I figure that pthreads should work just as well on both machines. 基本上,下面的代码旨在在Linux上使用,也许这就是我使用Windows XP时出现错误的原因,但我认为pthreads在两台计算机上都应该工作得很好。 I'm using gcc as my compiler and I did link with -lpthread but I got the following error anyways. 我使用gcc作为编译器,并且确实与-lpthread链接,但无论如何我都遇到了以下错误。

|21|undefined reference to sched_setaffinity'| |30|undefined reference to | 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 sched_setaffinity的sched_setaffinity'| |30|undefined reference to '|

If there is another method to setting the thread affinity using pthreads (on windows) let me know. 如果还有另一种使用pthreads设置线程相似性的方法(在Windows上),请告诉我。 I already know all about the windows.h thread affinity functions available but I want to keep things multiplatform. 我已经了解了所有可用的windows.h线程相似性函数,但是我想使事情保持多平台。 thanks. 谢谢。

#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() and sched_setaffinity() are strictly Linux-specific calls. sched_getaffinity()sched_setaffinity()严格来说是特定于Linux的调用。 Windows provides its own set of specific Win32 API calls that affect scheduling. Windows提供了自己的一组特定的Win32 API调用,这些调用会影响计划。 See this answer for sample code for Windows. 有关Windows的示例代码,请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM