Linux kernel 如何用kthread实现线程互斥?

Linux kernel 如何用kthread实现线程互斥?,linux-kernel,Linux Kernel,我知道我们可以使用pthread\u mutex\u init和pthread\u mutex\u lock来实现线程互斥。但是如何在内核模块中用kthread实现它呢?您不能使用pthread\u mutex.*函数,因为它们是仅用于用户空间的调用。在内核中,使用以下提供的互斥体: 你提供的链接对我很有用,非常感谢! struct mutex my_mutex; /* shared between the threads */ mutex_init(&my_mutex); /* ca

我知道我们可以使用
pthread\u mutex\u init
pthread\u mutex\u lock
来实现线程互斥。但是如何在内核模块中用
kthread
实现它呢?

您不能使用
pthread\u mutex.*
函数,因为它们是仅用于用户空间的调用。在内核中,使用以下提供的互斥体:


你提供的链接对我很有用,非常感谢!
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);