zoukankan      html  css  js  c++  java
  • 临界区互斥使用之使用自旋锁

    #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;
    }
    

      

    一勤天下无难事。
  • 相关阅读:
    Elasticsearch 分词
    Elasticsearch:文档乐观锁控制 if_seq_no与if_primary_term
    调用javaAPI访问hive
    sqoop笔记
    hive学习
    添加用户到sudo组
    HTTP协议用的TCP但是只建立单向连接
    Hadoop基本操作
    Hadoop原理介绍
    sed用法
  • 原文地址:https://www.cnblogs.com/nowroot/p/13624891.html
Copyright © 2011-2022 走看看