<Linux> 创建workqueue和thread

#include <linux/namei.h>
#include <linux/mount.h>
#include <linux/fd.h> 
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/time.h>

static void my_add_work_func(struct work_struct *work)
{ 
// do something
}
struct workqueue_struct *my_wq;
static DECLARE_WORK(my_wq_work, my_add_work_func);

void call(void) 
{
	if (my_wq != NULL)
		queue_work(my_wq, &my_wq_work);
}
void init()
{
	my_wq = create_singlethread_workqueue("my_wq");
}

void exit()
{
	flush_workqueue(my_wq);
}

static int my_thread(void *dir)
{
	// do something
	return 0;
}

int call_thread(void)
{
	struct task_struct *thread;
	thread = kthread_run(my_thread, (void*)NULL, "my_thread");

	if (IS_ERR(thread)) {
		pr_err("failed to create thread for keyon_thread init\n");
		return -EBUSY;
	}    
        return 0;
}

猜你喜欢

转载自blog.csdn.net/meta_cpp/article/details/79214239