如何使用kthread实现线程互斥锁? [英] How to implement thread mutex with kthread?

查看:131
本文介绍了如何使用kthread实现线程互斥锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know that we can use pthread_mutex_init and pthread_mutex_lock to implement thread mutual exclusion. But how can I implement it in kernel module with kthread?

推荐答案

您不能使用pthread_mutex_*函数,因为这些是仅限用户空间的调用.在内核中,使用 linux/mutex提供的互斥锁. h :

You cannot use the pthread_mutex_* functions as these are userspace-only calls. In the kernel use the use the mutexes provided by 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);

这篇关于如何使用kthread实现线程互斥锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆