如何从pthreads中的每个线程按顺序打印?

Hot*_*ace 1 c posix pthreads

我正在研究一个类的程序,它采用Floyd-Warshall有向图(由矩阵表示)并为图中的每个节点创建一个新的距离矩阵,将使用pthreads在线程之间创建新矩阵的工作分开.我觉得我到底是对的,我可以得到矩阵来形成和打印,但我似乎无法弄清楚如何按顺序打印距离矩阵(首先由线程0创建的矩阵行,然后线程1,线程2等).我使用互斥锁允许每个线程一起打印它的部分而不会中断,我只是无法按顺序打印线程.

我想知道那里的pthread guru是否会帮助我.提前致谢!

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

int n, totaln, **C, **D;                    /* Variable declarations */
pthread_t *threads;
pthread_mutex_t mutexprint;
long thread, threadcount;

void *LowestTerm(void* rank);

int main(int argc, char *argv[]) {

    int i, j, k;                        /* Variable declarations */
    char filename[50];

    threadcount = atoi(argv[1]);
    threads = malloc (threadcount * sizeof(pthread_t));

    printf("Enter filename: ");             /* User enters filename for directed graph values */
    scanf("%s", filename);

    FILE *fp = fopen(filename, "r");

    if (fp == NULL) {                   /* Check whether file exists or not */
        printf("File does not exist");
        return 1;
    }

    fscanf(fp, "%d", &n);                   /* Obtain size of matrix */

    totaln = n * n;

    C = (int **)malloc(n * sizeof(int *));          /* Allocate memory for matrix arrays */
    D = (int **)malloc(n * sizeof(int *));

    for (i = 0; i < n; i++) {               /* Allocate matrices into 2D arrays */
        C[i] = (int *)malloc(n * sizeof(int));
        D[i] = (int *)malloc(n * sizeof(int));
    }


    for (i = 0; i < n; i++) {               /* Read matrix from file into C array */
        for (j = 0; j < n; j++) {
            fscanf(fp, "%d", &C[i][j]);
        }
    }

    printf("Cost Adjacency Matrix:\n");         /* Print cost adjacency matrix */
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            printf("%d ", C[i][j]);
        }
        printf(" \n");
    }

    for (i = 0; i < n; i++) {               /* Copy matrix from C array into D array */
        for (j = 0; j < n; j++) {
            D[i][j] = C[i][j];
        }
    }

    printf("Distance matrix:\n");



    for (thread = 0; thread < threadcount; thread++) {
        pthread_create(&threads[thread], NULL, LowestTerm, (void*) thread);
    }
    for (thread = 0; thread < threadcount; thread++) {
        pthread_join(threads[thread], NULL);
    }

    pthread_mutex_destroy (&mutexprint);
    free(threads);
    pthread_exit(NULL);

}


void *LowestTerm(void* rank) {

    int i, j, k;                        /* Variable declarations */
    long mythread = (long) rank;

    int istart = ((int)mythread * n) / (int)threadcount;    /* Create matrix row start and finish parameters for each thread */
    int ifinish = ((((int)mythread + 1) * n) / (int)threadcount);

    for (k = 0; k < n; k++) {               /* Find shortest path for each value in each row for each of designated thread's rows */
        for (i = istart; i < ifinish; i++) {
            for (j = 0; j < n; j++) {
                if (D[i][j] > D[i][k] + D[k][j]) {
                    D[i][j] = D[i][k] + D[k][j];
                }
            }
        }
    }

    pthread_mutex_lock (&mutexprint);           /* Print distance matrix */
    for (i = istart; i < ifinish; i++) {
        printf("Thread %d: ", mythread);
        for (j = 0; j < n; j++) {
            printf("%d ", D[i][j]);
        }
        printf(" \n");
    }
    pthread_mutex_unlock (&mutexprint);


    return NULL;
}
Run Code Online (Sandbox Code Playgroud)

caf*_*caf 5

最简单的解决方案就是让所有工作线程完成后,主线程按照您想要的顺序打印整个矩阵.


或者,要按顺序打印线程,可以使用指定要打印的下一个线程的共享变量(初始化为0),并与条件变量配对:

pthread_mutex_lock (&mutexprint);           /* Print distance matrix */
while (next_thread != mythread)
    pthread_cond_wait(&condprint, &mutexprint);

for (i = istart; i < ifinish; i++) {
    printf("Thread %d: ", mythread);
    for (j = 0; j < n; j++) {
        printf("%d ", D[i][j]);
    }
    printf(" \n");
}

next_thread++;
pthread_cond_broadcast(&condprint);
pthread_mutex_unlock (&mutexprint);
Run Code Online (Sandbox Code Playgroud)

作为一个单独的问题,我不认为你的线程安全地共享D[]数组 - 看起来读取D[k][j]可能正在读取由另一个线程同时写入的位置.