0

问题:我必须增加 x1 和 x2 变量,这应该由单独的线程完成,并且两个变量的下一个增量不应该被调用,直到两个变量的前一个增量都没有完成。

问题:

x1 =  0;  x2 = 0;

x1++; and x2++; should run parallel on different threads and wait for printing.
should print: x1 = 1 and x2 = 1 
x1++; and x2++; should run parallel on different threads and wait for printing.
should print: x1 = 2 and x2 = 2 
x1++; and x2++; should run parallel on different threads and wait for printing.
should print: x1 = 3 and x2 = 3 
x1++; and x2++; should run parallel on different threads and wait for printing.
should print: x1 = 4 and x2 = 4
    …
    …
    …
x1++; and x2++; should run parallel on different threads and wait for printing.
should print: x1 = 10 and x2 = 10

close the threads

使用 pthread 条件的建议解决方案:初始化 4 个互斥锁和 4 个条件变量,并使用 2 个互斥锁锁定主函数并为每个线程休息。两个线程都将等待主函数传递条件信号来调用它们,并在计算后将信号传递回主线程以进一步移动。提供 1 秒的睡眠,要求线程正确调用并准备好在计算后接收和返回信号。

Pthread条件代码:

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

    pthread_t pth1,pth2;
    //Values to calculate
    int x1 = 0, x2 = 0;

    pthread_mutex_t m1, m2, m3, m4 = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t c1, c2, c3, c4 = PTHREAD_COND_INITIALIZER;

    void *threadfunc1(void *parm)
    {
        pthread_mutex_lock(&m1);
        for (;;) {
            pthread_cond_wait(&c1, &m1);
            x1++;
            pthread_mutex_lock(&m3);
            pthread_cond_signal(&c3);
            pthread_mutex_unlock(&m3);
        }
        pthread_mutex_unlock(&m1);
        return NULL ;
    }
    void *threadfunc2(void *parm)
    {
        pthread_mutex_lock(&m2);
        for (;;) {
            pthread_cond_wait(&c2, &m2);
            x2++;
            pthread_mutex_lock(&m4);
            pthread_cond_signal(&c4);
            pthread_mutex_unlock(&m4);
        }
        pthread_mutex_unlock(&m2);
        return NULL ;
    }

    int main () {
        pthread_create(&pth1, NULL, threadfunc1, "foo");
        pthread_create(&pth2, NULL, threadfunc2, "foo");
        sleep(1);
        int loop = 0;
        pthread_mutex_lock(&m3);
        pthread_mutex_lock(&m4);
        while (loop < 10) {
            // iterated as a step
            loop++;
            printf("Initial   : x1 = %d, x2 = %d\n", x1, x2);
            pthread_mutex_lock(&m1);
            pthread_cond_signal(&c1);
            pthread_mutex_unlock(&m1);
            pthread_mutex_lock(&m2);
            pthread_cond_signal(&c2);
            pthread_mutex_unlock(&m2);
            pthread_cond_wait(&c3, &m3);
            pthread_cond_wait(&c4, &m4);
            printf("Final   : x1 = %d, x2 = %d\n", x1, x2);
        }
        printf("Result   : x1 = %d, x2 = %d\n", x1, x2);
        pthread_mutex_unlock(&m3);
        pthread_mutex_unlock(&m4);
        pthread_cancel(pth1);
        pthread_cancel(pth2);
        return 1;
    }

使用信号量的建议解决方案:初始化 4 个信号量并调用单独的线程以单独增加变量。2个信号量用于将消息传递给线程以开始递增,2个信号量用于将消息传递给主线程以完成递增。主线程将等待来自两个子线程的信号量发布,显示两个变量的增量已完成,然后主线程将消息传递给两个子线程,允许进一步增量。

信号量代码:

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

    //Threads
    pthread_t pth1,pth2;

    //Values to calculate
    int x1 = 0, x2 = 0;

    sem_t c1,c2,c3,c4;

    void *threadfunc1(void *parm)
    {
        for (;;) {
            x1++;
            sem_post(&c1);
            sem_wait(&c3);
        }
        return NULL ;
    }

    void *threadfunc2(void *parm)
    {
        for (;;) {
            x2++;
            sem_post(&c2);
            sem_wait(&c4);
        }
        return NULL ;
    }

    int main () {
        sem_init(&c1, 0, 0);
        sem_init(&c2, 0, 0);
        sem_init(&c3, 0, 0);
        sem_init(&c4, 0, 0);
        pthread_create(&pth1, NULL, threadfunc1, "foo");
        pthread_create(&pth2, NULL, threadfunc2, "foo");
        sem_wait(&c1);
        sem_wait(&c2);
        sem_post(&c3);
        sem_post(&c4);
        int loop = 0;
        while (loop < 8) {
            // iterated as a step
            loop++;
            printf("Initial   : x1 = %d, x2 = %d\n", x1, x2);
            sem_wait(&c1);
            sem_wait(&c2);
            printf("Final   : x1 = %d, x2 = %d\n", x1, x2);
            sem_post(&c3);
            sem_post(&c4);
        }
        sem_wait(&c1);
        sem_wait(&c2);
        sem_destroy(&c1);
        sem_destroy(&c2);
        sem_destroy(&c3);
        sem_destroy(&c4);
        printf("Result   : x1 = %d, x2 = %d\n", x1, x2);
        pthread_cancel(pth1);
        pthread_cancel(pth2);
        return 1;
    }

请建议我,哪一种是更好的实施方式或我可以在哪里改进?任何类型的建议都会有所帮助。提前致谢。对不起,如果我重复自己,因为,这个问题已经成为我的噩梦......请帮助。

4

0 回答 0