linux多线程同步设计

前言

  多线程的开发,不是我们随便将想要并发的内容一股脑写完各干各的就ok了,会有同步和异步的需求,比如我们不知道什么时候要来消息需要阻塞监听时可以用异步,如果我们有稳定的信息源,并且需要在同等间隔的时间内做出高性能的处理时,同步的设计会更恰当一点,多线程之间的同步除了共享内存,常见的还有锁和信号量,单纯的FLAG置位通知什么的信号量也能轻易做到,但是频繁加锁解锁也会对性能造成影响,选择一个恰当的方式有利于程序的稳定运行,这里介绍一种基础的利用std queue的方式同步运行的方式

程序设计

  比如我们三个线程需要同步,第一个线程只负责获取图像数据,为了最大程度发挥3559的特性,我们需要将各自的线程绑定到指定的cpu核,预留性能最好的核用来处理算法。那么第一个线程,我们可以绑定到比如cpu0,只源源不断的获取图像数据,获取到后保存至队列Q1,而第二个线程呢,绑定到cpu1,接着while1去读Q1的是否为空,如果有我们就去处理,处理完丢给队列Q2,同理线程3绑定到cpu0(不和线程2用一个就行),while1去读队列Q2,不为空接着处理,处理完如果有需要丢给下一个队列…这样每个线程之间,由于处理速度不一样,有了队列作为缓冲,就可以有条不紊的进行运作(当然处理时间也不能太长,极端的例子比方说线程3最后输出过慢,从线程1获取的资源迟迟不释放,像海思平台很快VB就会满了,线程1就获取不到数据了,再大的缓冲池也扛不住)整个时间线切起来大概就是下图这样
在这里插入图片描述

线程绑定

  线程绑定指定cpu才可以做到上述设想,不然像海思2个A73,2个A53切起来才不会管时间片分给谁合适,线程执行的函数添加下面部分可以绑定,

 cpu_set_t mask;//cpu核的集合
 cpu_set_t get;//获取在集合中的cpu

    int num = sysconf(_SC_NPROCESSORS_CONF);
    printf("frame_check_task:system has %d processor(s)\n", num);

    CPU_ZERO(&mask);//置空
    CPU_SET(0, &mask);//设置亲和力值
     
    if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)//设置线程CPU亲和力
    {
    
    
        fprintf(stderr, "set thread affinity failed\n");
    }

    if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0)//获取线程CPU亲和力
    {
    
    
        fprintf(stderr, "get thread affinity failed\n");
    }

  如果编译报错不认识CPU_ZERO,在头文件中添加__USE_GNU即可,有位置需求,必须在#include <sched.h>之前

#define __USE_GNU
#include <sched.h>
#include <pthread.h>

参考代码

/* 
 *描述  :线程里用于检查是否有图像
 *参数  :arg 为自定义结构video_process_s,VPSS_GRP和VPSS_CHN用于传参给HI_MPI_VPSS_GetChnFrame
 *返回值:无
 *注意  :HI_MPI_VPSS_GetChnFrame完必须释放,否则再次获取VB会报错
          VIDEO_FRAME_INFO_S里的虚拟地址不可以直接使用,必须通过物理地址HI_MPI_SYS_Mmap映射后才可以作为数据存放地址用
          用完同样需要HI_MPI_SYS_Munmap解映射      
 */

HI_VOID *frame_check_task(HI_VOID *arg)
{
    
    
    cpu_set_t mask;//cpu核的集合
    cpu_set_t get;//获取在集合中的cpu

    int num = sysconf(_SC_NPROCESSORS_CONF);
    printf("frame_check_task:system has %d processor(s)\n", num);

    CPU_ZERO(&mask);//置空
    CPU_SET(0, &mask);//设置亲和力值
     
    if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)//设置线程CPU亲和力
    {
    
    
        fprintf(stderr, "set thread affinity failed\n");
    }

    if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0)//获取线程CPU亲和力
    {
    
    
        fprintf(stderr, "get thread affinity failed\n");
    }
    HI_S32 s32Ret;
    
    // VIDEO_FRAME_INFO_S* pstVideoFrame = &stVideoFrame_process;
    VIDEO_FRAME_INFO_S stVideoFrame_check;
    video_process_s* pstPara;
    pstPara = (video_process_s*)arg;
    sleep(1);
    hi_memset(&stVideoFrame_check,sizeof(VIDEO_FRAME_INFO_S),0,sizeof(VIDEO_FRAME_INFO_S));
    // queue<int> q;
	// for(int i = 0; i < 5; i++){
    
    
	// 	q.push(i);
	// }
	// cout<<q.front()<<' '<<q.back();
    while(1)
    {
    
    
        // sleep(1);
        s32Ret = HI_MPI_VPSS_GetChnFrame(pstPara->VpssGrp, pstPara->VpssChn, &stVideoFrame_check,1000);
        // SAMPLE_PRT("pstPara->VpssGrp is %dpstPara->VpssChnis %d!\n",pstPara->VpssGrp, pstPara->VpssChn);//while1打印太多
        if(s32Ret != HI_SUCCESS)
        {
    
    
            
             usleep(1000);
             SAMPLE_PRT("VPSS_GetChnFrame err for %#x!\n",s32Ret);//while1打印太多
        }
        // printf("u32TimeRef is %d\r\n",pstVideoFrame->stVFrame.u32TimeRef);
        else
        {
    
    
            // SAMPLE_PRT("frame_check_task:VPSS_GetChnFrame success\n");//while1打印太多
            // frm_recv_flag = 1;
            // sem_post(&sem_frm_recv);
            q_stVideoFrame_process.push(stVideoFrame_check);
            // HI_MPI_VPSS_ReleaseChnFrame(pstPara->VpssGrp, pstPara->VpssChn, &stVideoFrame_check);
            // printf("check%d\n", q_stVideoFrame_process.size());
            //  printf("check ok\n");
        }
    } 
    return NULL; 
}
/* 
 *描述  :线程里用于处理图像图像
 *参数  :arg 为自定义结构video_process_s,VPSS_GRP和VPSS_CHN用于传参给HI_MPI_VPSS_GetChnFrame
 *返回值:无
 *注意  :     
 */
HI_VOID *frame_process_task(HI_VOID *arg)
{
    
    
    cpu_set_t mask;//cpu核的集合
    cpu_set_t get;//获取在集合中的cpu

    int num = sysconf(_SC_NPROCESSORS_CONF);
    printf("frame_process_task:system has %d processor(s)\n", num);

    CPU_ZERO(&mask);//置空
    CPU_SET(1, &mask);//设置亲和力值
     
    if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)//设置线程CPU亲和力
    {
    
    
        fprintf(stderr, "set thread affinity failed\n");
    }

    if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0)//获取线程CPU亲和力
    {
    
    
        fprintf(stderr, "get thread affinity failed\n");
    }
    VIDEO_FRAME_INFO_S stVideoFrame_process;
    while(1)
    {
    
    
        // sleep(1);
        // sem_wait(&sem_frm_recv);
        if(q_stVideoFrame_process.empty() != true)
        {
    
    
            // printf("queue not empty\n");
            stVideoFrame_process = q_stVideoFrame_process.front();
            q_stVideoFrame_process.pop();
            // printf("process%d\n", q_stVideoFrame_process.size());
            /* 图像处理 */
            usleep(15000);
            q_stVideoFrame_process_osd.push(stVideoFrame_process);
            // printf("process ok\n");
            // sem_post(&sem_frm_process); 
        }
        else
        {
    
    
            usleep(1000);
        }
     
        
    }
     return NULL; 
}
 


/* 
 *描述  :线程里用于字符叠加
 *参数  :arg 为自定义结构video_process_s,VPSS_GRP和VPSS_CHN用于传参给HI_MPI_VPSS_GetChnFrame
 *返回值:无
 *注意  :  
 */

HI_VOID *frame_osd_task(HI_VOID *arg)
{
    
    
    cpu_set_t mask;//cpu核的集合
    cpu_set_t get;//获取在集合中的cpu

    int num = sysconf(_SC_NPROCESSORS_CONF);
    printf("frame_osd_task:system has %d processor(s)\n", num);

    CPU_ZERO(&mask);//置空
    CPU_SET(0, &mask);//设置亲和力值
     
    if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)//设置线程CPU亲和力
    {
    
    
        fprintf(stderr, "set thread affinity failed\n");
    }

    if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0)//获取线程CPU亲和力
    {
    
    
        fprintf(stderr, "get thread affinity failed\n");
    }

    VIDEO_FRAME_INFO_S stVideoFrame_process_osd;
    video_process_s* pstPara;
    pstPara = (video_process_s*)arg;
    
    // HI_S32 s32Ret;
    time_t now;
    struct tm *ptm;
    char timestr[OSD_LENGTH] = {
    
    0};
    char lost_target_info_str[OSD_LENGTH] = {
    
    0};
    char other_info_str[OSD_LENGTH] = {
    
    0};
    char trk_str[OSD_LENGTH] = {
    
    0};
    char frame_str[OSD_LENGTH] = {
    
    0};

    stBitmap_bottom_left.pData = malloc(2*OSD_BOTTOM_LEFT_W*OSD_BOTTOM_LEFT_H);
    if(stBitmap_bottom_left.pData == NULL)
    {
    
    
    printf("stBitmap.pData faided\r\n");
    }
    stBitmap_top_left.pData = malloc(2*OSD_TOP_LEFT_W*OSD_TOP_LEFT_H);
    if(stBitmap_top_left.pData == NULL)
    {
    
    
    printf("stBitmap_top_left.pData faided\r\n");
    }
    
    osd_init(arg);
	// int timeOffset = 0;
	// int timeOffset1 = 0;
	
    while(1)
    {
    
        
        // sleep(1);
        // sem_wait(&sem_frm_process);
        if(q_stVideoFrame_process_osd.empty() != true)
        {
    
    
            // tmp = clockGetTime(CLOCK_MONOTONIC);	//单位us
	        // timeOffset = tmp.tv_sec*1000*1000 + tmp.tv_usec;/* 测试代码执行时间 */
            // printf("osd queue not empty\n");
            stVideoFrame_process_osd = q_stVideoFrame_process_osd.front();
            q_stVideoFrame_process_osd.pop();
            // printf("osd%d\n", q_stVideoFrame_process_osd.size());
        
            time(&now);
            ptm = localtime(&now);
            snprintf(timestr,100,"当前时间:%d-%02d-%02d %02d:%02d:%02d  ",ptm->tm_year+1900,ptm->tm_mon+1,ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec); 
            snprintf(lost_target_info_str,100,"脱靶信息:X:%02d,Y:%02d",ptm->tm_year,ptm->tm_year);
            snprintf(other_info_str,100,"视场角信息:X:%02d,Y:%02d,焦距:%02d",ptm->tm_year,ptm->tm_year,ptm->tm_year);
            snprintf(trk_str,100,"跟踪器状态:检测?识别?跟踪? ");
            snprintf(frame_str,100,"帧率:%02d",ptm->tm_year);
            string_to_bmp_bottom_left(lost_target_info_str,other_info_str,timestr);
            string_to_bmp_top_left(trk_str,frame_str);
            HI_MPI_RGN_UpdateCanvas(OverlayHandle_top_left);
            HI_MPI_RGN_UpdateCanvas(OverlayHandle_bottom_left);
            // pthread_mutex_lock(&mutex);
            HI_MPI_RGN_SetBitMap(OverlayHandle_bottom_left,&stBitmap_bottom_left);//s32Ret 为RGN_HANDLE OverlayHandle
            HI_MPI_RGN_SetBitMap(OverlayHandle_top_left,&stBitmap_top_left);//s32Ret 为RGN_HANDLE OverlayHandle
            // pthread_mutex_unlock(&mutex);
            // usleep(15000);

            // memset(&tmp,0,sizeof(tmp));
            // tmp = clockGetTime(CLOCK_MONOTONIC);
            // timeOffset1 = tmp.tv_sec*1000*1000 + tmp.tv_usec;
            // printf("timeOffset:%d\n",(timeOffset1 - timeOffset));/* 测试代码执行时间 */

            // hi_memset(stBitmap_bottom_left.pData, sizeof(BITMAP_S),0, sizeof(BITMAP_S));
            // hi_memset(stBitmap_top_left.pData, sizeof(BITMAP_S),0, sizeof(BITMAP_S));
            // hi_memset(timestr,OSD_LENGTH,0,OSD_LENGTH);

            // q_stVideoFrame_venc.push(stVideoFrame_process_osd);
            // printf("osd ok\n");
            HI_MPI_VENC_SendFrame(pstPara->VpssChn, &stVideoFrame_process_osd,1000);
            HI_MPI_VPSS_ReleaseChnFrame(pstPara->VpssGrp, pstPara->VpssChn, &stVideoFrame_process_osd);
        }
        else
        {
    
    
            usleep(1000);
        }      
    }
    return NULL;   
}

猜你喜欢

转载自blog.csdn.net/qq_42330920/article/details/127070983