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 8017465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:51:07+00:00 2026-06-04T20:51:07+00:00

I am new to C++ Programming as well as thread implementation. My goal was

  • 0

I am new to C++ Programming as well as thread implementation. My goal was to design a program that uses two threads to add the elements in 2 subranges (elements 0-9 and elements 10-19) of one array, then add the values returned by the threads to formulate the sum of all the elements of the array. I’ve complied the following code and based on my limited knowledge of the “gdb” debugger it seems my issue is with the pointers in sum_function. I cannot figure out my mistake. Any help is appreciated!!!

#include <iostream>
#include <pthread.h>

using namespace std;

int arguments[20]; 

void *sum_function (void *ptr);

int main (void) {

pthread_t thread1, thread2;
int total, sum1, sum2 = 0;
int lim1 = 10;
int lim2 = 20;
for (int i = 0; i < 20; i++)
    cin >> arguments[i];

sum1 = pthread_create ( &thread1, NULL, sum_function, (void*) lim1);
sum2 = pthread_create ( &thread2, NULL, sum_function, (void*) lim2);

pthread_join (thread1, NULL);
pthread_join (thread2, NULL);

total = sum1 + sum2;

cout << "OUTPUT \n" << total << "\n";

return (0);
}

void *sum_function (void *lim) {

int sum = 0;
for (int j = 0; j < (*(int*)lim); j++)
    sum += arguments[j];
return (void*) sum;

}
  • 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-04T20:51:08+00:00Added an answer on June 4, 2026 at 8:51 pm
    sum1 = pthread_create ( &thread1, NULL, sum_function, (void*) lim1);
    sum2 = pthread_create ( &thread2, NULL, sum_function, (void*) lim2);
    

    This passes 10 and 20, cast to a void * to the threads.

    for (int j = 0; j < (*(int*)lim); j++)
    

    This casts the 10 and 20 to an int * and then dereferences them. But they’re not valid pointers.

    If you want the thread to receive an address, you have to pass it an address. If you want to pass the thread a value, code it to receive a value.

    You can fix this two ways:

    1) Consistently pass and expect pointers:

    sum1 = pthread_create ( &thread1, NULL, sum_function, (void*) &lim1);
    sum2 = pthread_create ( &thread2, NULL, sum_function, (void*) &lim2);
    ...
    for (int j = 0; j < (*(int*)lim); j++)
    

    Notice that pthread_create is now passing the thread a pointer, and the thread is now dereferencing that pointer.

    2) Consistently pass and expect values:

    sum1 = pthread_create ( &thread1, NULL, sum_function, (void*) lim1);
    sum2 = pthread_create ( &thread2, NULL, sum_function, (void*) lim2);
    ...
    for (int j = 0; j < ((int)lim); j++)
    

    Notice that pthread_create is now passing an integer value, and the thread is now expecting an integer value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to programming. I have a form application that launches a thread. Form
hi I am new here and new in Java programming as well actually my
I'm very much new to programming and have been doing fairly well so far.
Well, I encountered this new term in programming. What is it? How does it
Hey there, I'm sort of new to Objective-C, and well, programming in general. I
let's say that you in your new programming work your boss comes and says
I always believed that when starting to learn a new programming language programmer must
I'm new to this page and new to programming as well. Let me pose
hello I am new here and in programming as well. Today I have been
Well, i am at new at Socket programming in java. I tried to implement

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.