Home > Back-end >  Multithreading but first few threads are being skipped
Multithreading but first few threads are being skipped

Time:12-10

It's been a few hours and i can't seem to understand the issue. Build this program to count from 1 - 10. The goal of this program is to use multithreading and dynamically split the array depending on how many threads it requested. Problem is the first 2 threads are being skipped and the last thread is doing most of th e process. I suspect it's the for loop that creates the threads.

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

typedef struct
{
    int *array;
    int batch;
    int start;
    int end;
} Parameter;

void *method(void *p)
{
    Parameter *param = (Parameter *)p;

    for (int i = param->start; i < param->end; i  )
    {
        printf("Start:%d\tEnd:%d\tIndex:%d\tValue:%d\n", param->start, param->end, i,param->array[i]);
        
    }
}

int main(int argc, char **argv)
{
    // Getting the user input
    int array_length = atoi(argv[1]);
    int batches = atoi(argv[2]);

    printf("User specified Array:%d\tBatch:%d\n", array_length, batches);

    // Creating an array
    int *array = (int *)calloc(array_length, sizeof(int));
    
    // Fill it up with some data
    for (int i = 0; i < array_length; i  )
    {
        array[i] = i;
    }

    // Determine the Batches
    int batch_size = array_length / batches;
    int remainder = array_length % batches;

    printf("%d\n", batch_size);
    printf("%d\n", remainder);

    int start = 0;
    int end = 0;
    int index =0;

    // List of parameters
    Parameter *param = (Parameter *)calloc(batches, sizeof(Parameter));
    pthread_t *threads = (pthread_t *)calloc(batches, sizeof(pthread_t));

    // Loop through each batch.
    for (int i = 0; i < batches; i  )
    {
        printf("\n\nBatch number -> %d\n", i);
        end = start   batch_size;
        if (remainder > 0)
        {
            remainder --;
            end   ;
        }

        // Fill the parameters
        param[i].array = array;
        param[i].end = end;
        param[i].start = start;
        param[i].batch = i;

        // Call the thread.
        pthread_create(threads   index, NULL, method, (void *)&param[i]);
        index  ;
        start = end;
    }

    for (int i = 0; i < batches; i  )
    {
        pthread_join(threads[i], NULL);
    }

    free(param);
    free(threads);
    free(array);

    return 0;
}

Been playing with the index of the for loop(line 57) as i'm certain it's the cause of the issue. been getting some results but the main problem still persisted.

CodePudding user response:

Code Works as intended. I'm a dumbas who didn't put the printf in the void function. like so:

void *method(void *p) {
Parameter *param = (Parameter *)p;
printf("\n\nBatch number -> %d\n", param->batch); //<-- moved from main method
for (int i = param->start; i < param->end; i  )
{
    printf("Start:%d\tEnd:%d\tIndex:%d\tValue:%d\n", param->start, param->end, i,param->array[i]);
    
} }

Thanks for pointing it out that the program works

  • Related