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
?
Asked
Active
Viewed 8,095 times
7

Fan Wu
- 169
- 2
- 11
1 Answers
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
-
1The link you offered is quite useful to me, Thank you very much! – Fan Wu Nov 01 '11 at 00:51