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

后端 未结 2 1519
攒了一身酷
攒了一身酷 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: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 .

提交回复
热议问题