I compile this code main.c in CentOS7 with gcc:
#include <pthread.h>
void* mystart(void* arg)
{
pthread_yield();
return(0);
}
int main(void)
{
pthread_t pid;
pthread_create(&pid, 0, mystart, 0);
return(0);
}
1st compile: gcc -Wall -g main.c -pthread -o a.out
It's all OK.
2nd compile: gcc -Wall -g main.c -lpthread -o a.out
Gives
warning: implicit declaration of function 'pthread_yield' [-Wimplicit-function-declaration]
a.out
still run correctly ?-pthread
? Is sched_yield
another way to yield a pthread ?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>
.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With