用例程解释create_singlethread_workqueue与create_workqueue的区别

使用create_singlethread_workqueue创建工作队列即使对于多CPU系统,内核也只负责在一个cpu上创建一个worker_thread内核线程;而使用create_workqueue创建工作队列对于多CPU系统,内核将会在每个CPU上创建一个worker_thread内核线程,使得线程处理的事务能够并行化.

用代码解释前先说明一个知识点:
  printk任何时候,任何地方都能调用它,可以在中断上下文和进程上下文中被调用,可以在任何持有锁时被调用;可以在多处理器上同时被调用,而且调用者连锁都不必使用

==========================================================================================

例程1:使用create_workqueue创建工作队列

复制代码

#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/sysfs.h>
#include <linux/ctype.h>
#include <linux/workqueue.h>
#include <linux/delay.h>

//工作以队列结构组织成工作队列(workqueue),其数据结构为workqueue_struct,
static struct workqueue_struct *test_wq = NULL;    

//把推后执行的任务叫做工作(work),描述它的数据结构为work_struct
static struct work_struct   work;

/*
 *定义工作队列调用函数
 */
void work_func(struct work_struct *work){

        while(1){
                printk(KERN_ERR "-----%s-----\n",__func__);  //printk可以在多处理器上同时被调用
        }
}


static int __init test_init(void){
    
        /*创建工作队列workqueue_struct,该函数会为cpu创建内核线程*/
        test_wq = create_workqueue("test_wq");   
        
        /*初始化工作work_struct,指定工作函数*/
        INIT_WORK(&work,work_func);

        /*将工作加入到工作队列中,最终唤醒内核线程*/
        queue_work(test_wq, &work);

        while(1){
                mdelay(1000);
                printk(KERN_ERR "-----%s-----\n",__func__);
        }

        return 0;
}


static void __exit test_exit(void){
    
}


module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("liujin725@outlook.com");

复制代码

运行结果:没有打印任何信息,系统直接卡死,卡死原因是因为所有的cpu都被printk占用,系统无法调用其他的进程

=============================================================================

例程2:使用create_singlethread_workqueue创建工作队列
 

复制代码

#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/sysfs.h>
#include <linux/ctype.h>
#include <linux/workqueue.h>
#include <linux/delay.h>

 //工作以队列结构组织成工作队列(workqueue),其数据结构为workqueue_struct,
static struct workqueue_struct *test_wq = NULL;   

//把推后执行的任务叫做工作(work),描述它的数据结构为work_struct
static struct work_struct   work;

/*
 *定义工作队列调用函数
 */
void work_func(struct work_struct *work){

        while(1){
                printk(KERN_ERR "-----%s-----\n",__func__);  //printk可以在多处理器上同时被调用
        }
}


static int __init test_init(void){
    
        /*创建工作队列workqueue_struct,该函数会为cpu创建内核线程*/
        test_wq = create_singlethread_workqueue("test_wq");   
        
        /*初始化工作work_struct,指定工作函数*/
        INIT_WORK(&work,work_func);

        /*将工作加入到工作队列中,最终唤醒内核线程*/
        queue_work(test_wq, &work);

        while(1){
                mdelay(1000);
                printk(KERN_ERR "-----%s-----\n",__func__);
        }

        return 0;
}


static void __exit test_exit(void){
    
}


module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("liujin725@outlook.com");

复制代码

运行结果:
<3>[ 124.050211] -----work_func-----   //work_func有打印
<3>[ 124.244364] -----work_func-----
<3>[ 124.341765] -----work_func-----
<3>[ 124.537000] -----work_func-----
<3>[ 124.630770] -----work_func-----
<3>[ 124.801644] -----test_init-----    //test_init有打印
<3>[ 124.825084] -----work_func-----



一直打印log

==========================================================================
总结:
使用create_workqueue创建的工作队列在工作执行函数work_func中循环调用printk会导致系统卡死,是因为create_workqueue创建工作队列时在每个cpu上都创建了worker_thread内核线程,worker_thread线程处理的事务能够并行化,导致所有的cpu都被printk函数所占用,系统无法调用其他的进程,所以系统出现卡死并且无任何log信息打印

而使用create_singlethread_workqueue创建的工作队列只在一个cpu上创建worker_thread内核线程,即使该cpu一直被printk占用也还有其他的cpu可以继续调用其他的进程,所以能够一直打印log

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我将给你提供一个使用 rt_mq_t 的例程。这个例程演示了如何创建一个消息队列、向消息队列中发送消息、从消息队列中接收消息,并且在接收到消息后将消息打印出来。请参考代码如下: ```c #include <rtthread.h> #define THREAD_PRIORITY 25 #define THREAD_STACK_SIZE 512 #define THREAD_TIMESLICE 5 static rt_mq_t mq; // 定义一个消息队列 static char mq_pool[1024]; // 定义消息队列使用的内存池 static void sender_entry(void *parameter) { rt_err_t result; rt_uint8_t data = 0; while (1) { /* 发送消息 */ result = rt_mq_send(mq, &data, sizeof(data)); if (result == RT_EOK) { rt_kprintf("send data:%d\n", data); data++; } rt_thread_mdelay(1000); // 延时1秒 } } static void receiver_entry(void *parameter) { rt_err_t result; rt_uint8_t data; while (1) { /* 接收消息 */ result = rt_mq_recv(mq, &data, sizeof(data), RT_WAITING_FOREVER); if (result == RT_EOK) { rt_kprintf("received data:%d\n", data); } } } int rt_application_init(void) { rt_thread_t sender_tid, receiver_tid; /* 创建消息队列 */ mq = rt_mq_create("mq", sizeof(rt_uint8_t), 32, &mq_pool); if (mq == RT_NULL) { return -1; } /* 创建发送线程 */ sender_tid = rt_thread_create("sender", sender_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); if (sender_tid != RT_NULL) { rt_thread_startup(sender_tid); } /* 创建接收线程 */ receiver_tid = rt_thread_create("receiver", receiver_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); if (receiver_tid != RT_NULL) { rt_thread_startup(receiver_tid); } return 0; } ``` 在这个例程中,我们首先定义了一个消息队列 `mq`,并且定义了一个大小为1024的内存池 `mq_pool` 用于存储消息队列中的消息。然后我们创建了两个线程:`sender` 和 `receiver`,它们分别用于向消息队列中发送消息和从消息队列中接收消息。在 `sender_entry` 函数中,我们使用 `rt_mq_send` 函数发送一个字节大小的数据,并且打印发送的数据;在 `receiver_entry` 函数中,我们使用 `rt_mq_recv` 函数接收一个字节大小的数据,并且打印接收到的数据。在 `rt_application_init` 函数中,我们首先创建了消息队列,然后创建了 `sender` 和 `receiver` 两个线程,并且启动它们。 希望这个例程能够帮到你。如果还有其他问题,请继续提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值