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
?
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);
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