我知道我们可以使用pthread_mutex_init
和pthread_mutex_lock
来实现线程互斥。但是如何在内核模块中使用kthread
实现它呢?
我知道我们可以使用pthread_mutex_init
和pthread_mutex_lock
来实现线程互斥。但是如何在内核模块中使用kthread
实现它呢?
你不能使用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);