Why implicit declaration of pthread_yield with -lpthread while all ok with -pthread?

后端 未结 2 1491
攒了一身酷
攒了一身酷 2021-01-17 22:02

I compile this code main.c in CentOS7 with gcc:

#include 
void* mystart(void* arg)
{
    pthread_yield();
    return(0);
}
int main(void)
{
         


        
相关标签:
2条回答
  • 2021-01-17 22:40

    You should use -pthread for compile and link. It not only links the library, it also sets preprocessor defines and sometimes selects a different runtime library (on Windows for example).

    0 讨论(0)
  • 2021-01-17 22:41

    pthread_yield() is a non-standard function which is typically enabled by defining

    #define _GNU_SOURCE
    

    While you should use -pthread for compiling, I would expect you to get the same warning with both compilations (unless -pthread defines _GNU_SOURCE which may be the case).

    The correct way to fix is to not use the non-standard function pthread_yield() and use the POSIX function sched_yield() instead by including #include <sched.h>.

    0 讨论(0)
提交回复
热议问题