0

I have an application using posix threads ie using a static library which has some global variables and I dont have any thread implementation in libray. Somebody told me you should use pthread_mutex if you are using pthreads in that file else simple mutex.

As library is thread free so which lock (specially mutex) to use in library to protect globals.

I tried linux/kernel/mutex.h ie obviously bad to use kernel's object to use in user space and compiler too warning. So where will I get this simple mutex!!!

linuxD
  • 229
  • 1
  • 3
  • 10

1 Answers1

0
#include<pthread.h>

int main(int argc, char** argv){
    pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_lock(&lock);
    pthread_mutex_unlock(&lock);
}

you can compile this with: gcc -o mutex program.c -pthread -Wall -Wextra -pedantic (note not -lpthread for an explanation see the answer of EmployedRussian in this thread: Undefined reference to pthread_create in Linux)

If you get this to compile you are there

Community
  • 1
  • 1
hetepeperfan
  • 4,292
  • 1
  • 29
  • 47