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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:52:47+00:00 2026-05-28T07:52:47+00:00

EDIT: Question closed. Suggested pow of elements int mypow(int n) { return n *

  • 0
EDIT: Question closed.

Suggested pow of elements


int mypow(int n) { return n * n; }

transform(datos->origen->begin(),datos->origen->end(),datos->cuads->begin(),mypow);

I am trying to get the sum , the min and pow over an int vector concurrently.
I am using ubuntu 11.10 , codelite and g++ with phtreads.

I fill the vector, and spawn a thread per task. But I got wrong result.

I think the code it’s ok, but really not and I don’t why.

Filling the int vector with 10 ints, I got the following execution:

Sum: 110
Min: 2
original container length:10
2
4
6
... to 20

pow container lenght:20
0
0
0
... 10 zeros

4  ----> true result after pow elements
16
36
64
... rest of elements

Main thread ended

Thanks in advance

code:

#include <iostream>
#include <vector>

#include <pthread.h>

using std::cout;
using std::cin;     // C++ uses
using std::endl;
using std::vector;


// struct to store results
typedef struct
{
int suma;
int min;
vector<int>* origen;
vector<int>* cuads;
}tdata;


// mutex
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;


// Adition funct
void *suma(void* ptr)
{

// lock mutex
pthread_mutex_lock( &mutex1 );

tdata* datos = reinterpret_cast<tdata*>(ptr);

// iterator
vector<int>::const_iterator it1 = datos->origen->begin();

while( it1 != datos->origen->end() )
{
    datos->suma += *it1;    
    it1++;
}

pthread_mutex_unlock( &mutex1 );

 return 0;
}

// minimun function
void* min(void *ptr)
{ 

pthread_mutex_lock( &mutex1 );

tdata* datos = reinterpret_cast<tdata*>(ptr);

datos->min = datos->origen->at(0); // inicializo el menor

vector<int>::const_iterator it2 = datos->origen->begin();

while( it2 != datos->origen->end())
{   
    if ( (*it2) < datos->min )
        datos->min = (*it2);

        it2++;  
}

pthread_mutex_unlock( &mutex1 );
    return 0;
 }

// pow function. Dinamically alloc vector and fill it
void* cuadrados( void* ptr)
 {

pthread_mutex_lock( &mutex1 );

tdata* datos = reinterpret_cast<tdata*>(ptr);

// Error int tan = static_cast<int>(datos->origen->size());

datos->cuads = new vector<int>();  // Error new vector<int>(tan);               

vector<int>::const_iterator it3 = datos->origen->begin();

while( it3 != datos->origen->end() )
{       
    int n = (*it3) * (*it3);
    datos->cuads->push_back(n);

    it3++;  
}   
pthread_mutex_unlock( &mutex1 );
  return 0;
 }



int main(int argc, char **argv)
{

#define MAXHILOS 3      // nº de hilos
#define MAXNUMS 10      // cantidad de numeros

vector<int> enteros;    // vector de enteros            

pthread_t hilos[MAXHILOS];  // vector de hilos


    // fill origin vector
    for ( int i = 0; i < MAXNUMS; i++)
            enteros.push_back((i+1)*2);


    // init target structure
    tdata t = {0};

    // point to origin vector
    t.origen = &enteros;

        // thread creation                                  
        pthread_create(&hilos[0],NULL,suma,&t);         
        pthread_create(&hilos[1],NULL,min,&t);
        pthread_create(&hilos[2],NULL,cuadrados,&t);

        // wait until all threads ends
        pthread_join(hilos[0], NULL);
        pthread_join(hilos[1], NULL);
        pthread_join(hilos[2], NULL);


        // show results         
        cout << "Sum: " << t.suma << endl
             << "Min: " << t.min << endl;

        cout << "original vector length:" << enteros.size() << endl;

        vector<int>::const_iterator ent = enteros.begin();

        while( ent != enteros.end() )
        {   
            cout << (*ent) << endl;                             
            ent++;  
        }

        cout << "pow vector length:" << t.cuads->size() << endl;            


        vector<int>::const_iterator cuadr =t.cuads->begin();

        while( cuadr != t.cuads->end() )
        {   
            cout << (*cuadr) << endl;                               
            cuadr++;    
        }


        //delete[] t.cuads;         
        cout << "Main thread ended" << endl;

cin.get();
return 0;
} 
EDIT: Yes, the trouble was creating the vector with fixed size.
Thanks to all. 
  • 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-05-28T07:52:47+00:00Added an answer on May 28, 2026 at 7:52 am

    I believe the problem is the ten zeros at the beginning of the tdata.cuads
    vector. This is caused by:

    // This allocates a new vector with 'tan' number of ints
    // and initialises them to zero.
    datos->cuads = new vector<int>(tan);                
    
    // This loops appends new elements to the 'cuads' vector,
    // after the ten zeros.
    while( it3 != datos->origen->end() )
    {       
        int n = (*it3) * (*it3);
        datos->cuads->push_back(n);
    
        it3++;  
    }   
    

    To correct this either declare create the cuads without an initial size:

    datos->cuads = new vector<int>();                
    
    while( it3 != datos->origen->end() )
    {       
        int n = (*it3) * (*it3);
        datos->cuads->push_back(n);
    
        it3++;  
    }   
    

    or use the operator[] to populate cuads:

    datos->cuads = new vector<int>(tan);                
    
    size_t i = 0;
    while( it3 != datos->origen->end() )
    {       
        int n = (*it3) * (*it3);
        *(datos->cuads)[i++] = n;
        it3++;  
    }   
    

    Few other minor points:

    • typedef is not required when defining a struct in C++, just struct tdata { ... };.
    • you could use std::accumulate() to calculate the sum of a container and std::min_element() to find the min value of container.

    For example:

    dataos->suma = std::accumulate(dataos->origen->begin(), dataos->origen->end(), 0);
    dataos->min  = *std::min_element(dataos->origen->begin(), dataos->origen->end());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: this question is mostly closed and the only problems i have with this
edit: this question was closed as a duplicate, but the identified duplicate was asked
EDIT This question has been closed on SO and reposted on ServerFault https://serverfault.com/questions/333168/how-can-i-make-my-ssis-process-consume-more-resources-and-run-faster I
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This question is more about language engineering than C++ itself. I used C++
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
EDIT : This question duplicates How to access the current Subversion build number? (Thanks
(EDIT: This question is now outdated for my particular issue, as Google Code supports
Edit This question has gone through a few iterations by now, so feel free
EDIT: This question is a duplicate of What is the difference between managed and

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.