Как отдать порядок выполнения pthreads в c? - PullRequest
0 голосов
/ 26 мая 2020

Есть три потока pthread.

, и я хочу добавить поток, который устанавливает порядок потоков.

в моем исходном коде, я получаю случайные результаты.

ex )

1 2 2 1 1 3 3 3

но я хочу, 1 1 1 2 2 2 3 3 3

как это сделать с помощью дополнительной pthread?

Спасибо за прочтение.

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

void *runner1(void *argument) {
    for (int i = 0; i < 3; i++) {
        printf("1 ");
    }
    return NULL;
}

void *runner2(void *argument) {
    for (int i = 0; i < 3; i++) {
        printf("2 ");
    }
    return NULL;
}

void *runner3(void *argument) {
    for (int i = 0; i < 3; i++) {
        printf("3 ");
    }
    return NULL;
}

int main() {
    pthread_t t1;
    pthread_t t2;
    pthread_t t3;

    pthread_create(&t1, NULL, runner1, NULL);
    pthread_create(&t2, NULL, runner2, NULL);
    pthread_create(&t3, NULL, runner3, NULL);

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    pthread_join(t3, NULL);

    return 0;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...