Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8837711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:47:51+00:00 2026-06-14T09:47:51+00:00

i have a program in C. I have created 3 threads with pthread_create and

  • 0

i have a program in C.

I have created 3 threads with pthread_create and i have created a mutex
in order to lock/unlock the critical regions.

The 3nd argument of pthread_create is a pointer to a function that the thread will execute.

In the examples i’ve found in the Web, this function is always very simple e.g. prints the thread id or prints a message.

What happens when the function that the thread shall execute contains a for loop?

Cause in my program i would like each one of the threads to work with a two dimensional array.

Each thread shall find the sum of a line of a two-dimensional array.
e.g.

Thread1 shall calculate the sum of first line of the 2-dimensional array

Thread2 shall calculate the sum of the second line
Thread1 shall calculate the sum of the 3nd line
Thread3 shall calculate the sum of the 3nd line

I don’t care about the order of the threads, but i need every
thread to pick one of the lines.

I have the following code that sums two cells in the two dimensional array.

The program:

  1. creates NTHREADS

     for(i=0; i < NTHREADS; i++)
        {
           pthread_create( &thread_id[i], NULL, CalculateSum, NULL );
        }
    
  2. Each thread waits for the others to finish

    for(j=0; j < NTHREADS; j++)
       {
          pthread_join( thread_id[j], NULL);
       }
    
  3. the function that each thread shall execute but for ONE line of the array and NOT for the WHOLE array is

    void *CalculateSum(void *dummyPtr)
    {
       pthread_mutex_lock( &mutex1 );
    
     int i,j,sum = 0;
    
      for( i = 0; i <= N-1; i++) {
            for( j = 0; j <= M-1; j++) {
                    sum = dimensional_array[i][j] + dimensional_array[i][j];
            }
             printf(" Sum = %d\n", sum);
            }
    
       counter++;
    
       pthread_mutex_unlock( &mutex1 );
    }
    

The whole program is as follows:
The program does not have any compilation error.

In order to run it you shall do: gcc -pthread program.c

    //program.c

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

   #define NTHREADS 3
   void *CalculateSum(void *);
   pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
   int  counter = 0;

   #define N 10
   #define M 10

   int dimensional_array[N][M];

   main()
   {
      pthread_t thread_id[NTHREADS];
      int i, j;

      for (i = 0; i <= N - 1; i++ )
           for( j = 0; j <= M - 1; j++)
                   dimensional_array[i][j] = i;

      for(i=0; i < NTHREADS; i++)
      {
         pthread_create( &thread_id[i], NULL, CalculateSum, NULL );
      }

      for(j=0; j < NTHREADS; j++)
      {
         pthread_join( thread_id[j], NULL);
      }



      printf("Final counter value: %d\n", counter);

      //print ARRAY
      for (i = 0; i <= N-1; i++ ) {
           for( j = 0; j <= M-1; j++)
                   printf("%d\t",dimensional_array[i][j]);
           printf("\n");
           }
   }
   //Calculate
   void *CalculateSum(void *dummyPtr)
   {
      pthread_mutex_lock( &mutex1 );

    int i,j,sum = 0;

     for( i = 0; i <= N-1; i++) {
           for( j = 0; j <= M-1; j++) {
                   sum = dimensional_array[i][j] + dimensional_array[i][j];
           }
            printf(" Sum = %d\n", sum);
           }

      counter++;

      pthread_mutex_unlock( &mutex1 );
   }

So, i would like each thread to find the sum of a line but i’m confused, i don’t know
how to do that.

In my program every time a thread calls the Calculate function, all the sum of the lines are computed and not just one

[Caution:For simplicity i sum the first element with it’s own,the point is to understand
how those threads can all take place in that for loop]

I would be glad if someone could help me

Thanks, in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:47:52+00:00Added an answer on June 14, 2026 at 9:47 am

    You should create an array of per-thread parameters, and pass these to the threads one-by-one. In your case a single pointer to int is sufficient: you pass to the thread its index threadindex from zero to NTHREADS, and the thread passes back the sum for rows such that row % NTHREADS == threadindex.

    Here is how your thread function looks:

    void *CalculateSum(void *args)
    {
        int *argPtr = args;
    
        int i,j,sum = 0;
        int threadindex = *argPtr;
    
        for( i = 0; i <= N-1; i++) {
            if (i % NTHREADS != threadindex) continue;
            for( j = 0; j <= M-1; j++) {
                sum += dimensional_array[i][j];
            }
        }
    
        pthread_mutex_lock( &mutex1 ); Mutex must go here
        counter++;
        pthread_mutex_unlock( &mutex1 );
        // Pass the value back:
        *argPtr = sum;
    }
    
    
    main()
    {
        pthread_t thread_id[NTHREADS];
        int thread_args[NTHREADS];
        int i, j;
    
        pthread_mutex_init(&mutex1, NULL);
    
        for (i = 0; i <= N - 1; i++ )
            for( j = 0; j <= M - 1; j++)
                dimensional_array[i][j] = i;
    
        for(i=0; i < NTHREADS; i++)
        {
            thread_args[i] = i;
            pthread_create( &thread_id[i], NULL, CalculateSum, &thread_args[i]);
        }
    
        int sum = 0;
        for(j=0; j < NTHREADS; j++)
        {
            pthread_join( thread_id[j], NULL);
            sum += thread_args[i];
        }
    
        printf("Final counter value: %d. Total: %d\n", counter, sum);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to refactor a Java program created with Eclipse RCP. My predecessor handed
I have created a program which allows me to upload images to my server.
I have created a program with Windows Forms in C# and the architecture is
Let's say I have created a program in C/C++ and have a source code.
I have created this simple program to learn shared_ptr using namespace std; #define Yes
I have created Excel Sheet using Java program. It works fine. My problem is,
I have created a non-form c# program that uses the NotifyIcon class. The text
I have created a vector of sets in my program, and I need to
I have created a concurrent, recursive directory traversal and file processing program, which sometimes
I'm writing a prime sieve program using eratosthenes' for class. I have 8 threads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.