如何用kthread实现线程互斥?

时间:2011-10-31 03:04:44

标签: linux-kernel

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

实现它

1 个答案:

答案 0 :(得分: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);
相关问题