如何使用kthread实现线程互斥?

7

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

1个回答

13

你不能使用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);

1
你提供的链接对我非常有用,非常感谢! - Fan Wu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接