# multi-thread for computing sum of numbers

Page 1 of 1

## 2 Replies - 15377 Views - Last Post: 04 October 2012 - 09:50 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294308&amp;s=291afd0c7207eee07fc75874b2fe30f9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 snowingCa

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 04-October 12

# multi-thread for computing sum of numbers

Posted 04 October 2012 - 02:45 PM

I have this program in C++, it count sum of one thread,I don't know how to make a sum of multi thread with a program divide the numbers from 1 to N into T groups of consecutive numbers.
For example, it has a 4 group, with N = 15, G = 4. TG1 = (1,2,3,4), G2 = (5,6,7,8), G3 = (9,10,11,12) and G4 = (13,14,15).
The output is ./sum 15 4
Thread 1 (1,4)
Thread 2 (5,8)
Thread 3 (9,12)
Thread 4 (13, 15)
Sum (1,15) = 120
Can anyone help me, please!.
Thanks in advance

--------------------------------------------

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

using namespace std;

// The adder thread that will add the number from 1 to n
void *adder(void * number);

// The sum variable that will be updated by all threads
unsigned long sum;

int main(int argc, char *argv[])
{
if (argc != 2) {
cerr << "Usage: sum <integer value>" << endl;
exit(1);
}

unsigned long number = atol(argv[1]);
if (number < 0) {
cerr << "Argument " << number << " must be non-negative." << endl;
exit(1);
}

pthread_t tid; // the thread identifierier
pthread_attr_t attr; // set of attributes for the thread

// get the default attributes
pthread_attr_init(&attr);

// create the thread
pthread_create(&tid,&attr,adder,(void *) number);

// Wait for the thread to exit
pthread_join(tid,NULL);

cout << "Sum(" << 1 << ", " << number << ") = " << sum << endl;

return 0;
}

// Add the numbers from 1 to n
void *adder(void * number)
{
cout << "Thread ID = " << pthread_self() << endl;

unsigned long num = (unsigned long) number;
for (int i = 1; i <= num; i++)
{
sum += i;
}

pthread_exit(0);
}
```

This post has been edited by jimblumberg: 04 October 2012 - 02:50 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Is This A Good Question/Topic? 0

## Replies To: multi-thread for computing sum of numbers

### #2 jimblumberg

Reputation: 5488
• Posts: 17,065
• Joined: 25-December 09

## Re: multi-thread for computing sum of numbers

Posted 04 October 2012 - 02:51 PM

Please avoid creating duplicate posts, and when posting code use code tags:

Jim
Was This Post Helpful? 0

### #3 Salem_c

• void main'ers are DOOMED

Reputation: 2161
• Posts: 4,228
• Joined: 30-May 10

## Re: multi-thread for computing sum of numbers

Posted 04 October 2012 - 09:50 PM

> Can anyone help me, please!.
So what is your real question?

Your simple code would need to solve these problems
- splitting your total into sub-ranges
- passing multiple parameters to a thread
- starting multiple threads
- collecting results from a thread without using global variables.

Well, here is an example of parameter passing and result returning involving threads.
You should study the use of new and delete carefully.
```#include <pthread.h>
#include <iostream>
#include <cstdlib>

using namespace std;

struct twonum {
int from, to;
twonum(int f, int t): from(f), to(t) { }
};

// The adder thread that will add the number from 1 to n
void *adder(void * number);

int main(int argc, char *argv[])
{
pthread_t tid; // the thread identifierier
pthread_attr_t attr; // set of attributes for the thread

// get the default attributes
pthread_attr_init(&attr);

twonum *p = new twonum(5,10);
// create the thread
pthread_create(&tid,&attr,adder,(void *)p);

// Wait for the thread to exit
int *sum;
pthread_join(tid,(void**)&sum);
cout << "Result=" << *sum << endl;
delete sum;

return 0;
}

// Add the numbers from 1 to n
void *adder(void * number)
{
twonum *p = (twonum*)number;
cout << "Thread ID = " << pthread_self() << endl;
cout << "Input=" << p->from << " " << p->to << endl;
int *result = new int;
*result = p->from + p->to;
delete p;
pthread_exit((void*)result);
}

```

Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }