zoukankan      html  css  js  c++  java
  • [国嵌攻略][088][多线程同步]

    线程同步

    多个线程按照规定的顺序来执行,即为线程同步。

    threadSync1.c

    #include <pthread.h>
    #include <stdio.h>
    #include <unistd.h>
    
    pthread_mutex_t mutex;
    int task = 0;
    
    void *thread0();
    void *thread1();
    
    void main(){
        //创建互斥锁
        pthread_mutex_init(&mutex, NULL);
        
        //创建子线程
        pthread_t thread[2];
        
        pthread_create(&thread[0], NULL, thread0, NULL);
        pthread_create(&thread[1], NULL, thread1, NULL);
        
        //等待子线程
        pthread_join(thread[0], NULL);
        pthread_join(thread[1], NULL);
    }
    
    void *thread0(){
        //处理任务
        int i;
        
        for(i = 0; i < 10; i++){
            //获取互斥锁
            pthread_mutex_lock(&mutex);
            
            //处理任务
            task++;
            printf("thread0 task:%d
    ", task);
            
            //释放互斥锁
            pthread_mutex_unlock(&mutex);
            
            //睡眠等待
            sleep(1);
        }
        
        //退出线程
        pthread_exit(NULL);
    }
    
    void *thread1(){
        //获取互斥锁
        pthread_mutex_lock(&mutex);
        
        //查看任务
        while(task != 10){
            //释放互斥锁
            pthread_mutex_unlock(&mutex);
            
            //睡眠等待
            sleep(1);
            
            //获取互斥锁
            pthread_mutex_lock(&mutex);
        }
        
        //处理任务
        task++;
        printf("thread1 task:%d
    ", task);
        
        //释放互斥锁
        pthread_mutex_unlock(&mutex);
    
        //退出线程
        pthread_exit(NULL);
    }

    threadSync2.c

    #include <pthread.h>
    #include <stdio.h>
    #include <unistd.h>
    
    pthread_mutex_t mutex;                            //互斥锁
    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;   //条件变量
    int task = 0;
    
    void *thread0();
    void *thread1();
    
    void main(){
        //创建互斥锁
        pthread_mutex_init(&mutex, NULL);
        
        //创建子线程
        pthread_t thread[2];
        
        pthread_create(&thread[0], NULL, thread0, NULL);
        pthread_create(&thread[1], NULL, thread1, NULL);
        
        //等待子线程
        pthread_join(thread[0], NULL);
        pthread_join(thread[1], NULL);
    }
    
    void *thread0(){
        //处理任务
        int i;
        
        for(i = 0; i < 10; i++){
            //获取互斥锁
            pthread_mutex_lock(&mutex);
            
            //处理任务
            task++;
            printf("thread0 task:%d
    ", task);
            
            //释放互斥锁
            pthread_mutex_unlock(&mutex);
            
            //睡眠等待
            sleep(1);
        }
        
        //条件通知
        pthread_cond_signal(&cond);
        
        //退出线程
        pthread_exit(NULL);
    }
    
    void *thread1(){
        //获取互斥锁
        pthread_mutex_lock(&mutex);
        
        //条件等待
        if(task != 10){
            pthread_cond_wait(&cond, &mutex);   //条件等待时会自动解锁,然后睡眠等待,获得条件通知再自动加锁
        }
        
        //处理任务
        task++;
        printf("thread1 task:%d
    ", task);
        
        //释放互斥锁
        pthread_mutex_unlock(&mutex);
    
        //退出线程
        pthread_exit(NULL);
    }
  • 相关阅读:
    反射
    如何通过反射调用对象的方法?
    简述一下面向对象的”六原则一法则”。
    用Java写一个单例类。
    什么是UML?
    UML中有哪些常用的图?
    用Java写一个折半查找。
    两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对?
    构造器(constructor)是否可被重写(override)?
    用最有效率的方法计算2乘以8?
  • 原文地址:https://www.cnblogs.com/d442130165/p/5229869.html
Copyright © 2011-2022 走看看