Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement thread mutex with kthread?

Tags:

linux-kernel

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?

like image 892
Fan Wu Avatar asked Oct 31 '11 03:10

Fan Wu


1 Answers

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);
like image 161
Mircea Avatar answered Oct 16 '22 19:10

Mircea