linux - 线程同步中的条件变量该如何理解 ?
阿神
阿神 2017-05-16 13:28:30
[Linux讨论组]

全局变量初始化为0, 两个线程, 一个线程对它每次增1, 当大于等于100时, 给另一个线程发送条件. 重置为0

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>


struct foo{
    int f_count;
    pthread_mutex_t f_lock;
    pthread_cond_t f_cond;
};

/* 初始化互斥量与条件变量 */
struct foo * foo_alloc(){
    struct foo *fp;
    if((fp = malloc(sizeof(struct foo))) != NULL){
        fp->f_count = 0;
        pthread_mutex_init(&fp->f_lock, NULL);
        pthread_cond_init(&fp->f_cond, NULL);
    }
    return fp;
}

/* 加法 */
void *foo_increase(void *arg){
    
    struct foo *fp;

    fp = (struct foo*)arg;

    while(1){
        pthread_mutex_lock(&fp->f_lock);
    
        fp->f_count++;
        /* 大于等于100时发送条件 */
        if(fp->f_count >= 100){
            pthread_cond_signal(&fp->f_cond);
        }

        pthread_mutex_unlock(&fp->f_lock);
    }
}

/* 重新置0 */
void *foo_print(void *arg){
    struct foo *fp;
    fp = (struct foo*)arg;

    while(1){
        pthread_mutex_lock(&fp->f_lock);
        
        while(fp->f_count < 100){
            //释放掉锁, 等待条件为真返回, 再次锁住.
            pthread_cond_wait(&fp->f_cond, &fp->f_lock);
        }
        printf("重置 : %d\n", fp->f_count);    
        /* 重新置0 */    
        fp->f_count = 0;
        pthread_mutex_unlock(&fp->f_lock);
    }
}


int main(void){

    struct foo *fp;
    pthread_t tid_increase1, tid_print;
    //初始化
    fp = foo_alloc();

    //加法线程
    pthread_create(&tid_increase1, NULL, foo_increase, fp);
    
    //重置线程
    pthread_create(&tid_print, NULL, foo_print, fp);

    //防止主线程提前退出
    sleep(20);
    exit(0);
}


可是我在重置线程下打印出来为什么是下面这个结果, 不应该是100吗 ?
重置线程已经把互斥量给锁住了, 但好像另一个线程仍然在不断累加.

置0前 : 54
置0前 : 92
置0前 : 47
置0前 : 85
置0前 : 51
...
阿神
阿神

闭关修行中......

全部回复(1)
PHP中文网
#include <stdio.h>

#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

struct foo{
    int f_count;
    pthread_mutex_t f_lock;
    pthread_cond_t f_cond;
};

/* 初始化互斥量与条件变量 */
struct foo * foo_alloc(){
    struct foo *fp;
    if((fp = (struct foo *)malloc(sizeof(struct foo))) != NULL){
        fp->f_count = 0;
        pthread_mutex_init(&fp->f_lock, NULL);
        pthread_cond_init(&fp->f_cond, NULL);
    }
    return fp;
}

/* 加法 */
void *foo_increase(void *arg){
    
    struct foo *fp;

    fp = (struct foo*)arg;

    while(1){
        
        pthread_mutex_lock(&fp->f_lock);
        
        fp->f_count++;
        
        if(fp->f_count >= 100){
            pthread_cond_signal(&fp->f_cond);
            
            //-------------------这里要等待,不然会再次循环+1
            pthread_cond_wait(&fp->f_cond, &fp->f_lock);
        }
        

        pthread_mutex_unlock(&fp->f_lock);
        
         /* 大于等于100时发送条件 */

    }
}

/* 重新置0 */
void *foo_print(void *arg){
    struct foo *fp;
    fp = (struct foo*)arg;

    while(1){
        pthread_mutex_lock(&fp->f_lock);
        
        while(fp->f_count < 100){
            
            //释放掉锁, 等待条件为真返回, 再次锁住.
            pthread_cond_wait(&fp->f_cond, &fp->f_lock);

        }
        printf("重置 : %d\n", fp->f_count);    
        /* 重新置0 */    
        fp->f_count = 0;
        
        //-------------------加这条就不会阻塞了foo_increase里的wait
        pthread_cond_signal(&fp->f_cond);
        
        pthread_mutex_unlock(&fp->f_lock);
    }
}


int main(void){

    struct foo *fp;
    pthread_t tid_increase1, tid_print;
    //初始化
    fp = foo_alloc();

    //加法线程
    pthread_create(&tid_increase1, NULL, foo_increase, fp);
    
    //重置线程
    pthread_create(&tid_print, NULL, foo_print, fp);

    //防止主线程提前退出
    sleep(2);这里这时要

    free(fp);//DO NOT FORGET

    exit(0);
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号