7

I know that we can use pthread_mutex_init and pthread_mutex_lock to implement thread mutual exclusion. But how can I implement it in kernel module with kthread?

Fan Wu
  • 169
  • 2
  • 11

1 Answers1

13

You cannot use the pthread_mutex_* functions as these are userspace-only calls. In the kernel use the use the mutexes provided by linux/mutex.h:

struct mutex my_mutex; /* shared between the threads */

mutex_init(&my_mutex); /* called only ONCE */

/* inside a thread */
mutex_lock(&my_mutex);
/* do the work with the data you're protecting */
mutex_unlock(&my_mutex);
Vittorio Cozzolino
  • 931
  • 1
  • 14
  • 31
Mircea
  • 1,841
  • 15
  • 18