首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >线程数组中的第一个线程在c中被跳过(有时)?

线程数组中的第一个线程在c中被跳过(有时)?
EN

Stack Overflow用户
提问于 2019-03-01 03:10:32
回答 1查看 199关注 0票数 0

我正在尝试用C语言编写一个多线程程序,它通过将数组分成多个分区来对它进行排序,然后每个线程都在自己的分区上工作。问题似乎是,有时线程1,有时线程2,在执行时会被跳过。我甚至还没有进行排序,只进行了比较。现在,我只想知道我的线程是否正常运行,但第一个线程似乎就是无法调度。我对C不是很在行,我真的在绞尽脑汁寻找可能出错的地方。

我知道每个线程都可以被调度程序暂停,但它们最终都应该完成,对吧?那么为什么我的前几个线程有时不能运行呢?

代码语言:javascript
复制
#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");
}

输出:(典型,当线程被跳过时)

代码语言:javascript
复制
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 -----
EN

回答 1

Stack Overflow用户

发布于 2019-03-01 04:08:43

不使用malloc/free的替代解决方案。

代码语言:javascript
复制
    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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54932676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档