我们可以删除等待使用while循环与其他任何东西的线程的全局变量?

时间:2013-08-22 07:00:09

标签: c multithreading mutex semaphore

Explanation of problem

在下面的代码中,我试图使用线程顺序计算x1和x2的值。(在实际情况中,x1和x2将是大计算) 但是等待两个线程使用while循环计算各个变量的值对于处理器而言变得昂贵。 问题是,我希望两个线程并行运行,但两个线程的循环应该是相同的序列化(手段应该在一次调用中运行一次)。 因此,有没有办法删除这些while循环并连续获得结果。我对使用信号量和互斥量非常困惑,因为x1和x2是独立的 彼此的?请帮忙。提前谢谢。

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

pthread_t pth1,pth2;
//Values to calculate
int x1 = 0, x2 = 0;
//Values for condition
int cond1 = 0,cond2 = 0;


void *threadfunc1(void *parm)
{
    for (;;) {
        // Is this while loop is very costly for the processor?
        while(!cond1) {}
        x1++;
        cond1 = 0;
    }
    return NULL ;
}
void *threadfunc2(void *parm)
{
    for (;;) {
        // Is this while loop is very costly for the processor?
        while(!cond2) {}
        x2++;
        cond2 = 0;
    }
    return NULL ;
}



int main () {
    pthread_create(&pth1, NULL, threadfunc1, "foo");
    pthread_create(&pth2, NULL, threadfunc2, "foo");
    int loop = 0;
    while (loop < 10) {
        // iterated as a step
        loop++;
        printf("Initial : x1 = %d, x2 = %d\n", x1, x2);
        cond1 = 1;
        cond2 = 1;
        // Is this while loop is very costly for the processor?
        while(cond1) {}
        while(cond2) {}
        printf("Final   : x1 = %d, x2 = %d\n", x1, x2);
    }

    pthread_cancel(pth1);
    pthread_cancel(pth2);
    return 1;
}

1 个答案:

答案 0 :(得分:2)

如果删除threadfunc1和threadfunc2中的while循环,则线程将返回而不继续。所以,你肯定需要一种方法让线程保持活着,直到它完成对系列的计算(在这种情况下为10)。

这是一个示例代码,显示了尝试在主线程和两个线程之间进行协调。使用pthread_cond_wait(),等待从简单的“while”循环移动到pthread_cond_wait()。

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

//Values to calculate
int x1 = 0, x2 = 0;
//Values for condition
int cond1 = 0, cond2 = 0;

static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t c = PTHREAD_COND_INITIALIZER;

void *threadfunc1(void *parm) {
    int i = 0;
    while (i < 10) {
        printf("\t\tThread1 with x1 = %d\n", x1);
        pthread_mutex_lock(&m1);
        if (cond1 == 0) {
            pthread_cond_wait(&c, &m1);
        }
        x1++;
        cond1 = 0;
        pthread_mutex_unlock(&m1);
        i++;
    }
    printf("\t\tThread1 returns with x1 = %d\n", x1);
    return NULL ;
}

void *threadfunc2(void *parm) {
    int i = 0;
    while (i < 10) {
        printf("\t\tThread2 with x2 = %d\n", x2);
        pthread_mutex_lock(&m2);
        if (cond2 == 0) {
            pthread_cond_wait(&c, &m2);
        }
        x2++;
        cond2 = 0;
        pthread_mutex_unlock(&m2);
        i++;
    }
    printf("\t\tThread2 returns with x2 = %d\n", x2);
    return NULL ;
}


int main () {
    pthread_t pth1, pth2;
    pthread_create(&pth1, NULL, threadfunc1, "foo");
    pthread_create(&pth2, NULL, threadfunc2, "foo");
    int retVal;
    int loop = 0;
    while (loop <= 10) {
        loop++;
        cond1 = 1;
        pthread_cond_signal(&c);

        cond2 = 1;
        pthread_cond_signal(&c);
        printf("Loop [%d]: x1 = %d, x2 = %d\n", loop, x1, x2);
    }

    retVal = pthread_join(pth1, NULL);
    retVal = pthread_join(pth2, NULL);
    printf("Final : x1 = %d, x2 = %d\n", x1, x2);
    return 0;
}