码迷,mamicode.com
首页 > 其他好文 > 详细

锁和数据打包

时间:2020-08-26 18:48:05      阅读:49      评论:0      收藏:0      [点我收藏+]

标签:while   code   stat   common   ati   error   amp   div   打包   

#include "common.h"

static pthread_t  thread_miccapture;
static pthread_t  thread_audioplay;
static int pthread_run = 0;
static pthread_t  thread_main;
static char* pusb_confi ;
static float i2c_buf = 0;


typedef  struct  mutex_lock_tag      // 后面加tag 
{
    pthread_mutex_t  mutex;
    int data;
}mutex_lock_t;    // 后面加_t 


static mutex_lock_t  data = {
    .mutex = PTHREAD_MUTEX_INITIALIZER,
    .data =10  // 需要保护的数据
};


static void * mic_capture_thread(void *arg);
static void * audio_play_thread(void *arg);

static void * mic_capture_thread(void *arg)
{

    int ret = pthread_equal(thread_main,thread_miccapture);
    if(ret > 0)
    {    
        printf("two the same thread\n");
    }
    else 
    {
        printf("diff in two thread\n");
    }

    
    while(pthread_run)
    {
    
        pthread_mutex_lock(&data.mutex);
        data.data  =  10;
        pthread_mutex_unlock(&data.mutex);        
        printf("mutex is %d\n",data.data);
        usleep(100000);
        
    }

}

static void * audio_play_thread(void *arg)
{
    int read_status = 1; 
    int ret = 0;
    while(pthread_run)
    {
      
       if(read_status)
       {
            
            //if(ret)
            {
            
                pthread_mutex_lock(&data.mutex);
                data.data = 11;
                pthread_mutex_unlock(&data.mutex);
                printf("data.data is %d\n",data.data);
                usleep(100000);
                
            }
            
       }
    
    }
    
}



void task_create(void)
{
    char* tmp[10] = { 0};
    int ret = -1;
    pthread_run = 1;
    //do 
    {
         //ret = pthread_mutex_init(&mutex,NULL); // 添加初始化锁
         printf("ret is %d\n",ret);
        ret = pthread_create(&thread_miccapture,
                                NULL,
                                mic_capture_thread,
                                NULL);
        if(ret<0)
        {
           perror("pthread_create failure\n");
           return ;
        }

        
          ret = pthread_create(&thread_audioplay,
                              NULL,
                              audio_play_thread,
                              NULL);
        if(ret<0)
        {
           perror("pthread_create failure\n");
           return ;
        }
        

        pthread_join(thread_miccapture,NULL);
          pthread_join(thread_audioplay,NULL);
        pthread_mutex_destroy(&data.mutex);
        
    }
}

(1) typedef struct 结构体命名 : 用tag
typedef 类型用_t
typedef struct mutex_lock_tag // 后面加tag
{
pthread_mutex_t mutex;
int data;
}mutex_lock_t; // 后面加_t


static mutex_lock_t data = {
.mutex = PTHREAD_MUTEX_INITIALIZER,
.data =10 // 需要保护的数据
};
(2)用结构体打包需要的同步的数据 ,用意明确..

 

锁和数据打包

标签:while   code   stat   common   ati   error   amp   div   打包   

原文地址:https://www.cnblogs.com/nowroot/p/13540234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!