繁体   English   中英

pthread互斥锁中的异常行为

[英]Unexpected behaviour in pthread mutexes

大家好,我定义了2个互斥锁:bar_mutex(在N条钢筋之间使用)和writer_mutex(在bar线程中使用观察者线程)。 作者互斥体被用来避免变量k_total的怪异值 ,我正在修改全局变量,但在某些特定时刻,该变量变为0.0,而另一个线程将其与该值一起使用。 为了避免这种情况,我添加了writer_mutex,但问题仍然存在 我究竟做错了什么? 我将粘贴代码的重要部分。

if (k_total < 1 && b->cm <=20) {
    b->cm = b->cm + 10; //Usando este valor calculamos el deltak
    /* printf("\nThread yendo hacia abajo\n"); */
    if (b->direction == UP) {
        b->direction = DOWN;
        changed_direction = true;
    } else {
        changed_direction = false;
    }
} else if(k_total > 1 && b->cm >=-20) {
    b->cm = b->cm - 10; //Usando este valor calculamos el deltak
    /* printf("\nThread yendo hacia arriba\n"); */
    if (b->direction == DOWN) {
        b->direction = UP;
        changed_direction = true;
    } else {
        changed_direction = false;
    }
}

在函数的这一部分中,将move_bar k_total设为0而不是正确的值...为了避免这种情况, 我添加了writer_mutex,但仍无法正常工作

在此处输入图片说明

在这里,您可以看到全局变量ktotal在某些时候被发送为0 ...我试图避免这种情况。

我将附加代码的主要部分。

void
start_threads(struct bar* bars) {
    int i;
    pthread_t threads[NUM_THREADS + 1];
    bars = (struct bar*)malloc(sizeof(struct bar) * NUM_THREADS);
    init_variables();
    pthread_mutex_init(&bar_mutex, NULL);
    pthread_mutex_init(&write_mutex, NULL);
    pthread_cond_init(&unstable_state, NULL);

    for (i = 1; i < NUM_THREADS + 1; i++) {
        fill_bar(&bars[i - 1], i);
    }

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    // creating the 4 threads that handle each bar
    for (i = 0; i < NUM_THREADS; i++) {
        pthread_create(&threads[i], &attr, move_bar, (void *)&bars[i]);
    }
    //thread used to check if system is stable and change its k_total
    pthread_create(&threads[NUM_THREADS], &attr, check_stable, (void*)bars);

    /* Wait for all threads to complete */
    for (i=0; i<NUM_THREADS + 1; i++) {
        pthread_join(threads[i], NULL);
    }
}

void*
check_stable(void* bars) {
    struct bar* wires = (struct bar*) bars ;
    while (true) {
        pthread_mutex_lock(&write_mutex);
        k_total = 0.0;
        for (int i = 0; i < NUM_THREADS; ++i) {
            k_total += getDeltaKValue(wires[i].cm);
        }

        char str[25];
        sprintf(str,"deltak=%lf", k_total);
        doPost("deltak",str);

        k_total = k_value + k_total;

        sprintf(str,"kparcial=%lf",k_value);
        doPost("kparcial",str);
        sprintf(str,"ktotal=%lf", k_total);
        doPost("ktotal",str);
        if ((double)k_total != (double)1.0) {
            unbalanced = true;
            pthread_cond_signal(&unstable_state);
        } else {
            unbalanced = false;
        }
        pthread_mutex_unlock(&write_mutex);
        sleep(1);
    }
}

void*
move_bar(void *bar) {
    struct bar* b = (struct bar*) bar;
    clock_t start = clock();
    bool changed_direction = false;
    while (true) {
        pthread_mutex_lock(&bar_mutex);
        while (unbalanced == false)
            pthread_cond_wait(&unstable_state, &bar_mutex);
        pthread_mutex_lock(&write_mutex);
        if (k_total < 1 && b->cm <=20) {
            b->cm = b->cm + 10; //Usando este valor calculamos el deltak
            /* printf("\nThread yendo hacia abajo\n"); */
            if(b->direction == UP) {
                b->direction = DOWN;
                changed_direction = true;
            } else {
                changed_direction = false;
            }
        } else if (k_total > 1 && b->cm >=-20) {
            b->cm = b->cm - 10; //Usando este valor calculamos el deltak
            /* printf("\nThread yendo hacia arriba\n"); */
            if(b->direction == DOWN) {
                b->direction = UP;
                changed_direction = true;
            } else {
                changed_direction = false;
            }
        }

        k_total = 0.0;
        for (int i = 0; i < NUM_THREADS; ++i) {
            k_total += getDeltaKValue(bars[i].cm);
        }
        char str[25];
        if ((double)k_total != (double)1.0) {
            unbalanced = true;
        } else {
            printf("\nBALANCED");
            unbalanced = false;
        }

        pthread_mutex_unlock(&write_mutex);

        if (changed_direction) {
            sleep(CHANGE_DIRECTION);
        }
        sleep(MOVEMENT_TIME);

        sprintf(str,"id=%ld&cm=%d",b->id, b->cm);
        doPost("barValue",str);

        /* printf("\nEnding thread %ld", b->id); */
        changed_direction = false;
        pthread_mutex_unlock(&bar_mutex);
        sleep(1); 
    }
    pthread_exit(NULL);
}

好的,问题不在这里。 该代码确实有效。 使用gdb,我看到有2 具有相同名称但引用不同的全局变量 ,因为我对同一个变量执行了2个malloc,并且我在2个不同的函数中使用了该变量来更新bot结构,而不仅仅是1。我只是删除了意外的malloc,所以一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM