Thread: threads problem

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    24

    threads problem

    Hello everyone !!

    I am trying to build a program that calculates the sum of integers between 0 and N, where N is a given integer. And I would like to do this using threads.

    The program takes 2 inputs: N and the number of threads.
    So if I give him successively the values 10 and 2, than the program will create 2 threads: the first one will calculate the sum from 0 to 5 and the second one will calculate the sum from 6 to 10.

    As a first step, I would like that every thread calculates a portion of this sum and display it.

    However, it seems that the threads I create, calculate the same portion of the total sum. And I don't really know how to fix it !!

    Here is my code to show you what I did :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    typedef struct {
      long long N_min; // lower limit
      long long N_max; // upper limit
    } arg_t;
    
    
    // function that calculates the sum of integers between lower limit and upper limit
    void *routine(void* arg){
      arg_t *my_arg = arg;
      long long sum = 0.0;
    
      printf("n_min_received = %lld\n", my_arg->N_min);
      printf("n_max_received = %lld\n", my_arg->N_max);
    
      for (long long i=my_arg->N_min; i<my_arg->N_max; i++){
        sum += i ;
      }
      printf("portion_sum = %lli\n", sum);
      pthread_exit(0);
    }
    
    int main(int argc, char **argv){
    
      if (argc < 3){
      printf("Usage: %s <sum length> <thread count>\n", argv[0]);
      exit(EXIT_SUCCESS);
      }
    
      if (atoi(argv[1]) < atoi(argv[2])){
      printf("sum length should be higher than thread count\n");
      exit(EXIT_SUCCESS);
      }
    
      long long sum_length = atoi(argv[1])+1;
      long long thread_count = atoi(argv[2]);
      long long interval_length = sum_length / thread_count;
    
      pthread_t tids[thread_count];
    
      for (long long i=0; i<thread_count; i++){
    
        // initialize thread attributes
        pthread_attr_t attr;
        pthread_attr_init(&attr);
    
        // define the interval of focus for the routine
        long long N_min = i*interval_length;
        long long N_max = (i+1)*interval_length;
        printf("n_min_given = %lld\n", N_min);
        printf("n_max_given = %lld\n", N_max);
        arg_t my_arg = {N_min, N_max};
    
        // thread creation
        pthread_create(&tids[i], &attr, routine, &my_arg);
      }
    
      for (long long i=0; i<thread_count; i++){
        pthread_join(tids[i], NULL);
      }
    }
    can somebody help me please ?
    Last edited by Ramsis94; 05-03-2020 at 10:09 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yeah, each of your my_args needs to be a unique pointer as well.

    Code:
    arg_t my_args[thread_count];
    
      for (long long i=0; i<thread_count; i++){
     
        // initialize thread attributes
        pthread_attr_t attr;
        pthread_attr_init(&attr);
     
        // define the interval of focus for the routine
        long long N_min = i*interval_length;
        long long N_max = (i+1)*interval_length;
        printf("n_min_given = %lld\n", N_min);
        printf("n_max_given = %lld\n", N_max);
        my_args[i] = {N_min, N_max};
     
        // thread creation
        pthread_create(&tids[i], &attr, routine, &my_args[i]);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    24
    ok thanks !!

    I still have this
    Code:
    test.c: In function ‘main’:
    test.c:55:18: error: expected expression before ‘{’ token
       55 |     my_args[i] = {N_min, N_max};
          |

  4. #4
    Registered User
    Join Date
    Apr 2020
    Posts
    24
    It is ok now, I should set struct values like this :
    Code:
     
    my_args[i] = (arg_t){N_min, N_max};

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. threads inside threads problem
    By Ivan Mili in forum C++ Programming
    Replies: 4
    Last Post: 08-22-2014, 09:07 AM
  2. Replies: 22
    Last Post: 12-14-2012, 11:00 AM
  3. 3 threads problem
    By lectrolux in forum C Programming
    Replies: 2
    Last Post: 05-14-2003, 11:15 AM
  4. problem using threads......?
    By stumpert in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2002, 07:37 AM
  5. Threads problem
    By Barjor in forum Windows Programming
    Replies: 1
    Last Post: 03-12-2002, 09:54 PM

Tags for this Thread