zoukankan      html  css  js  c++  java
  • 互斥锁问题

    由于先锁A 所以先打印B

    先锁B就先打印A,为什么不是先打印B?

    #include <stdio.h>  
    #include <pthread.h>  
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    pthread_mutex_t A,B;
    pthread_mutex_init();
    void * fun1(void)  
    {
     int i = 0;
     for (;i<15;i++)
     {
        pthread_mutex_lock(&B);   
        printf("A");
        pthread_mutex_unlock(&A);            
        }
       return NULL;
    }
    void * fun2(void)  
    {
     int i = 0;
     for (;i<15;i++)
     {
        pthread_mutex_lock(&A);    
        printf("B");
        pthread_mutex_unlock(&B);            
        }
       return NULL;
    }
    int main(int argc, char *argv[])  
    {  
            pthread_t id1;  
            pthread_t id2;  
    
            pthread_mutex_init(&B,NULL);
            pthread_mutex_init(&A,NULL);
            pthread_mutex_lock(&A);
            pthread_create(&id1,NULL,(void *)fun1,NULL);
            pthread_create(&id2,NULL,(void *)fun2,NULL);
            pthread_join(id1,NULL);
            pthread_join(id2,NULL);
          
       
           //pthread_mutex_destory(&A);
                            
          // pthread_mutex_destory(&B);
            printf("
    ");
            return 0;
    }
    

    一个行者的旅途
  • 相关阅读:
    在线考试————随机出题
    HTTP协议
    团队
    做作业
    图书馆管理说明书性能
    关于敏捷开发的学习
    运行环境
    图书馆管理系统说明书
    性能(2)
    作业
  • 原文地址:https://www.cnblogs.com/xinzghewanfu/p/5904268.html
Copyright © 2011-2022 走看看