Implicit declaration of function 'pthread_mutex_init' invalid in C99

I am trying to block a method using mutec from this article here it claims to create a member variable of the class as such

pthread_mutex_t mutex; 

Then initialize it as such

  pthread_mutex_init(&mutex, NULL); 

Then use it as such

 void MyLockingFunction() { pthread_mutex_lock(&mutex); // Do work. pthread_mutex_unlock(&mutex); } 

I get the following warning in step 2 when I initialize it.

 Implicit declaration of function 'pthread_mutex_init' is invalid in C99 

What does it mean? Should I ignore him?

+5
source share
1 answer

This means that you did not include the header file that declares this function, so the compiler does not know anything about it at the point where you use it. You are trying to implicitly declare it using it, which is invalid.

If you check the man page for pthread_mutex_init() , it tells you that you should use the following line to import the declaration:

 #include <pthread.h> 

If you put this at the top of the source file, the warning will disappear.

+7
source

All Articles