Первые потоки в массиве потоков пропускаются в c (иногда)? - PullRequest
0 голосов
/ 28 февраля 2019

Я пытаюсь написать многопоточную программу на C, которая сортирует массив, разбивая его на разделы, а затем каждый поток работает на своем собственном разделе.Проблема заключается в том, что иногда поток 1, а иногда поток 2 пропускаются при выполнении.Я даже не попал на сортировку, только сравнение.Пока я просто хочу знать, что мои потоки работают правильно, но кажется, что первые могут просто не быть запланированы или что-то в этом роде.Я не очень силен с Си, и я действительно ломаю голову над тем, что может пойти не так.

Я знаю, что каждый поток может быть приостановлен планировщиком, но все они в конечном итоге должны быть завершены правильно?Так почему же мои первые пару потоков иногда не запускаются?

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

#define SIZE 11
#define HM_SORTERS 4

void * sort(void * param);
void printArr(int * arr);

struct PartitionBounds {
    int tid;
    size_t start;
    size_t end;
};

int array[SIZE] = {18, 12, 68, 59, 75, 73, 68, 4, 16, 94, 15};

int main() {

    // srand(time(NULL));
    // for (size_t i = 0; i < SIZE; i++) {
    //     array[i] = rand() % 100;
    // }

    printArr(array);

    pthread_t sorters[HM_SORTERS];

    for(int i=0;i<HM_SORTERS;i++) {
        struct PartitionBounds bounds;
        size_t coverage = SIZE / HM_SORTERS;
        bounds.tid = i;
        bounds.start = i * coverage;
        bounds.end = i + 1 == HM_SORTERS ? SIZE - 1 : bounds.start + coverage - 1;
        int status = pthread_create(&sorters[i], NULL, sort, &bounds);
        if(status == 0) {
            printf("\n--- Thread %d created successfully ---\n", i + 1);
            printf("start: %zu, end: %zu\n", bounds.start, bounds.end);
        } else {
            printf("!!! Failed to create thread !!!\n");
            return 0;
        }
    }

    for(int i=0;i<HM_SORTERS;i++) {
        pthread_join(sorters[i], NULL);
    }

    printf("\n\n----- Sorting completed -----\n\n");

    return 0;
}

void * sort(void * param) {
    struct PartitionBounds *bounds = param;
    printf("\n\tin thread %d\n\n", bounds->tid + 1);
    for(size_t i=bounds->start;i<=bounds->end;i++) {
        for(size_t j=i+1;j<=bounds->end;j++) {
            if(array[j] < array[i]) {
                printf("Thread %d: %d is smaller than %d\n", bounds->tid + 1, array[j], array[i]);
            }
        }
    }
    pthread_exit(NULL);
}

void printArr(int * arr) {
    int coverage = SIZE / HM_SORTERS;
    for(int i=0;i<HM_SORTERS;i++) {
        size_t partitionEnd = i + 1 == HM_SORTERS ? coverage + SIZE % HM_SORTERS : coverage;
        for(int j=0;j<partitionEnd;j++) {
            printf("%d", array[j + coverage * i]);
            if(j+1 < partitionEnd) {
                printf(", ");
            }
        }
        if(i+1 < HM_SORTERS) {
            printf(" | ");  
        }
    }
    printf("\n");
}

вывод: (обычно, когда потоки пропускаются)

18, 12 | 68, 59 | 75, 73 | 68, 4, 16, 94, 15

--- Thread 1 created successfully ---
start: 0, end: 1

--- Thread 2 created successfully ---
start: 2, end: 3

        in thread 1


--- Thread 3 created successfully ---
Thread 3: 73 is smaller than 75

        in thread 3

Thread 3: 73 is smaller than 75

        in thread 2

start: 4, end: 5
Thread 3: 73 is smaller than 75
Thread 4: 68 is smaller than 75
Thread 4: 4 is smaller than 75
Thread 4: 16 is smaller than 75
Thread 4: 15 is smaller than 75

--- Thread 4 created successfully ---
start: 6, end: 10
Thread 4: 68 is smaller than 73
Thread 4: 4 is smaller than 73
Thread 4: 16 is smaller than 73
Thread 4: 15 is smaller than 73
Thread 4: 4 is smaller than 68
Thread 4: 16 is smaller than 68
Thread 4: 15 is smaller than 68
Thread 4: 15 is smaller than 16
Thread 4: 15 is smaller than 94

        in thread 4

Thread 4: 4 is smaller than 68
Thread 4: 16 is smaller than 68
Thread 4: 15 is smaller than 68
Thread 4: 15 is smaller than 16
Thread 4: 15 is smaller than 94


----- Sorting completed -----

Ответы [ 2 ]

0 голосов
/ 28 февраля 2019

Альтернативное решение без использования malloc/free.

    struct PartitionBounds bounds[HM_SORTERS];
    ...
        size_t coverage = SIZE / HM_SORTERS;
        bounds[i].tid = i;
        bounds[i].start = i * coverage;
        bounds[i].end = i + 1 == HM_SORTERS ? SIZE - 1 : bounds[i].start + coverage - 1;
        int status = pthread_create(&sorters[i], NULL, sort, &bounds[i]);
    ...
0 голосов
/ 28 февраля 2019

Одна проблема, которую я вижу здесь, связана с адресом локальной переменной.

    struct PartitionBounds bounds;
    int status = pthread_create(&sorters[i], NULL, sort, &bounds);

bounds - локальная переменная, и она исчезнет, ​​и на каждой итерации будет создан новый экземпляр.Таким образом, ссылка на нее в sort будет иметь неопределенное поведение .


Что вы можете сделать, это динамически распределить память.

 struct PartionBounds *bounds = malloc(sizeof(*bounds));
 int status = pthread_create(&sorters[i], NULL, sort, bounds);

Makeуверен, что вы free память однажды сделали.

...