• 临界区互斥使用之使用自旋锁


    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <pthread.h>
    #include <semaphore.h>
    
    
    static int  pthread_run = 1;
    static void  print(char*s);
    static pthread_spinlock_t spin_lock;
    
    static void  read_wifi_message(void)
    {
       printf("read_wifi_message
    ");
    }
    
    static void write_wifi_messgage(void)
    {
        printf("write wifi messgage
    ");
    }
    static  void  critic_data(void)
    {
         read_wifi_message();
         if(1)
         {
           write_wifi_messgage();
         }
         printf("current sensor gain
    ");
         printf("vedio current signal
    ");
    }
    
    static void* Z_M_Pthread(void *arg)
    {
        int a ;
        while(pthread_run)
        {
            pthread_spin_lock(&spin_lock);
            critic_data();
            pthread_spin_unlock(&spin_lock);
    
        }
    
        return (void*)NULL;
    }
    
    static void* M_S_Pthread(void *arg)
    {
        int a ;
        while(pthread_run)
        {
            pthread_spin_lock(&spin_lock);
            critic_data();
            pthread_spin_unlock(&spin_lock);
    
        }
    
        return (void*)NULL;
    }
    
    // 定义一个临界区资源
    static void  print(char*s)
    {
       if(s==NULL)
       {
          printf("int value error
    ");
          return 0;
       }
       printf("I'm here %s
    ",s);
    }
    
    int main()
    {
    
    
        printf("EXIT_SUCCESS is %d
    ",EXIT_SUCCESS);
        printf("EXIT_FAILURE is %d
    ",EXIT_FAILURE);
        int ret = -1;
        pthread_t  z_mtid,m_stid;
    
        //pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);
        pthread_spin_init(&spin_lock,PTHREAD_PROCESS_PRIVATE);
        printf("ret is %d
    ",ret);
        ret = pthread_create(&z_mtid,NULL,Z_M_Pthread,NULL);
        printf("ret is %d
    ",ret);
        ret = pthread_create(&m_stid,NULL,M_S_Pthread,NULL);
        printf("ret is %d
    ",ret);
    
        pthread_join(z_mtid,NULL);
        pthread_join(m_stid,NULL);
        pthread_spin_destroy(&spin_lock);
    
        while(1);
    
        return 0;
    }
    

      

    一勤天下无难事。
  • 相关阅读:
    试试主题显示
    四则运算
    实验四 决策树算法及应用
    实验三 朴素贝叶斯算法及应用
    实验二 K-近邻算法及应用
    实验一 感知器及其应用
    实验三 面向对象分析与设计
    实验二 结构化分析与设计
    实验一 软件开发文档与工具的安装与使用
    举例分析流程图与活动图的区别与联系
  • 原文地址:https://www.cnblogs.com/nowroot/p/13624891.html
Copyright © 2020-2023  润新知