如何用kthread实现线程互斥?

Fan*_* Wu 6 linux-kernel

我知道我们可以使用pthread_mutex_initpthread_mutex_lock实现线程互斥.但是如何在内核模块中实现它kthread呢?

Mir*_*cea 12

您无法使用这些pthread_mutex_*功能,因为这些是仅限用户空间的呼叫.在内核中使用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);
Run Code Online (Sandbox Code Playgroud)