程序员

嵌入式系统开发小白学习笔记22

作者:admin 2021-06-22 我要评论

嵌入式系统开发学习笔记 嵌入式系统开发 链表 1、任务队列 2、线程池 example 嵌入式系统开发 链表 1、任务队列 1任务队列为空 2任务队列为满 3任务队列为不为空...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

嵌入式系统开发

链表

1、任务队列

(1)任务队列为空
(2)任务队列为满
(3)任务队列为不为空

任务队列为空时,线程池里的线程阻塞等待
任务队列不为空时,线程池里的线程处理任务
任务队列为满,不能再添加新的任务

2、线程池

(1)线程池里的线程数量
(2)线程池里的工作线程数量
(3)任务队列的大小
(4)任务队列锁

example:

#include  <stdio.h>
#include  <stdlib.h>
#include  <string.h>
#include  <pthread.h>
#include  <unistd.h>


struct job
{
    void *(*func)(void *arg);
    void *arg;
    struct job *next;
    
};

struct  threadpool
{
    int thread_num;//已开启的线程数量   
    pthread_t  *pthread_ids;//保存线程池中线程id

    struct job  *head;//任务队列的头指针
    struct job  *tail;//任务队列的尾指针
    int queue_max_num;//任务队列的最大数
    int queue_cur_num;//任务队列已有多少任务

    pthread_mutex_t  mutex;
    pthread_cond_t  queue_empty;//任务队列为空的条件
    pthread_cond_t  queue_not_empty;//任务队列部位空的条件
    pthread_cond_t  queue_not_full;//任务队列不为满的条件

};
//线程函数
void  *threadpool_function( void * arg)
{
    struct  threadpool  *pool =  (struct threadpool  *) arg  ;
    struct job *pjob = NULL;

    while(1)
    {
        pthread_mutex_lock( &(pool->mutex) );

        while(pool->queue_cur_num == 0)             //任务队列是否为空
        {
            pthread_cond_wait( &(pool->queue_not_empty), &(pool->mutex));
        }

        pjob = pool->head;
        pool->queue_cur_num --;

        if(pool->queue_cur_num != pool->queue_max_num)
        {
            pthread_cond_broadcast( &(pool->queue_not_empty) );
        }

        if ( pool->queue_cur_num==0 )
        {
            pool->head = pool->tail = NULL ;
            pthread_cond_broadcast(&(pool->queue_not_empty));
        }
        else
        {
            pool->head = pool->head->next ;
        }

        pthread_mutex_unlock( &(pool->mutex) );

        (*(pjob->func))(pjob->arg);
        free(pjob);
        pjob = NULL ;
        
    }

}
//线程池初始化
struct threadpool *threadpool_init( int thread_num,int queue_max_num)
{
    struct threadpool  *pool=( struct threadpool * )malloc(  sizeof(struct threadpool)  );
    //malloc
    //int thread_num = 20;
    //int queue_max_num = 100;

    pool->thread_num = thread_num ;
    pool->queue_max_num = queue_max_num;
    pool->queue_cur_num = 0;
    pool ->head = NULL;
    pool->tail = NULL;

    pthread_mutex_init( &(pool->mutex) ,NULL );
    pthread_cond_init( &(pool->queue_empty), NULL );
    pthread_cond_init( &(pool->queue_not_empty) , NULL );
    pthread_cond_init( &(pool->queue_not_full) , NULL );

    pool->pthread_ids =  (pthread_t  *) malloc( sizeof(pthread_t) * thread_num );
    //malloc

    for ( int i = 0; i < pool->thread_num ; i++)
    {
        pthread_create( &(pool->pthread_ids[i ]) , NULL , threadpool_function , (void  *)pool );
    }

    return pool;
}
//添加任务函数
void threadpool_add_job(struct threadpool *pool,void *func(void *) , void * arg )
{
    pthread_mutex_lock(&(pool->mutex));
    while(pool->queue_cur_num == pool->queue_max_num)
    {
        pthread_cond_wait( &(pool->queue_not_full), &(pool->mutex) );
    }

    struct job  *pjob = (struct  job *)malloc ( sizeof(struct  job));
    //malloc
    pjob->func =  func ;
    pjob->arg = arg;
    pjob->func(pjob->arg);

    if(NULL == pool->head)
    {
        pool->head = pool->tail =pjob;
        pthread_cond_broadcast(&(pool->queue_not_empty) );
    }
    else
    {
        pool->tail->next = pjob;
        pool->tail = pjob;
    }
    
    pool->queue_cur_num ++;

    pthread_mutex_unlock( &(pool->mutex) );
}

void thread_destroy(struct threadpool *pool)
{
    pthread_mutex_lock(&(pool->mutex));
    while(pool->queue_cur_num != 0)
    {
        pthread_cond_wait(&(pool->queue_empty),&(pool->mutex));
    }
    pthread_mutex_unlock(&(pool->mutex ));
    pthread_cond_broadcast(&(pool->queue_not_empty));
    pthread_cond_broadcast(&(pool->queue_not_empty));
    pthread_cond_broadcast(&(pool->queue_not_full));

    free(pool->pthread_ids);
    
    for(int i = 0;i < pool->thread_num;i++)
    {
        pthread_cancel(pool->pthread_ids[i]);
        pthread_join(pool->pthread_ids[i],NULL);
    }
    struct job *temp;
    while(pool->head != NULL)
    {
        temp = pool->head;
        pool->head = temp->next;
        free(temp);
    }
    free(pool);


}


//任务函数
void * work ( void * arg )
{
    char * p =(char *)arg;
    printf("hello world   %s\n",p);
    printf("welcome to china   %s\n",p);
    sleep(1);
}

int main()
{
    struct threadpool *pool = threadpool_init(10, 100);
    threadpool_add_job(pool,work,"1");
    threadpool_add_job(pool,work,"2");
    threadpool_add_job(pool,work,"3");
    threadpool_add_job(pool,work,"4");
    threadpool_add_job(pool,work,"5");
    threadpool_add_job(pool,work,"6");
    threadpool_add_job(pool,work,"7");
    threadpool_add_job(pool,work,"8");
    threadpool_add_job(pool,work,"9");
    threadpool_add_job(pool,work,"10");

    threadpool_add_job(pool,work,"11");
    threadpool_add_job(pool,work,"22");
    threadpool_add_job(pool,work,"33");
    threadpool_add_job(pool,work,"44");
    threadpool_add_job(pool,work,"55");
    threadpool_add_job(pool,work,"66");
    threadpool_add_job(pool,work,"77");
    threadpool_add_job(pool,work,"88");
    threadpool_add_job(pool,work,"99");
    threadpool_add_job(pool,work,"100");

    sleep(20);
    
    
    return 0;
}

注释:
int thread_num; //已开启的线程
struct job *head;//任务队列的头
struct job *tail; //任务队列的尾
int queue_max_num;//任务队列的最大数
int queue_cur_num;//任务队列已有多少个任在这里插入代码片
pthread_t *pthread_ids;//保存线程池中线程的id
pthread_mutex_t mutex;//定义任务锁
pthread_cond_t queue_empty;//任务队列为空
pthread_cond_t queue_not_empty;//任务队列不为空
pthread_cond_t queue_not_full;//任务队列不为满

;原文链接:https://blog.csdn.net/m0_52251623/article/details/115623098

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 阿里巴巴DevOps实践指南(八)| 以特性

    阿里巴巴DevOps实践指南(八)| 以特性

  • 阿里巴巴DevOps实践指南(五)| 业务驱

    阿里巴巴DevOps实践指南(五)| 业务驱

  • RISC-V工具链简介

    RISC-V工具链简介

  • 变局时代:RISC-V处理器架构的技术演变

    变局时代:RISC-V处理器架构的技术演变

腾讯云代理商
海外云服务器