2012-01-23 30 views
0
 
EDIT: Question closed. 

元素錯誤的結果處理矢量<int>與線程

 

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

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

我想獲得的總和,最小和戰俘在一個int載體的同時建議戰俘。 我正在使用Ubuntu 11.10,codelite和g ++與phtreads。

我填充矢量,併爲每個任務產生一個線程。但是我得到了錯誤的結果。

我認爲這個代碼沒問題,但真的沒有,我也不明白爲什麼。

充入10個整數的INT載體,我得到了以下執行:

 
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 

在此先感謝

代碼:

#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. 
+0

有什麼問題造成的? – hmjd

+0

@hmjd額外的零。此外,沒關係。 – ppk

回答

2

我相信問題是在tdata.cuads vector開頭的十個零。

// 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++; 
} 

要糾正此兩種申報創建cuads沒有初始大小:

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

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

    it3++; 
} 

或使用operator[]填充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++; 
} 

其他一些小的這是造成分數:

  • typedef在C++中定義struct時不需要,只需要struct tdata { ... };
  • 您可以使用std::accumulate()來計算容器和std::min_element()的總和以查找容器的最小值。

例如:

dataos->suma = std::accumulate(dataos->origen->begin(), dataos->origen->end(), 0); 
dataos->min = *std::min_element(dataos->origen->begin(), dataos->origen->end()); 
+0

你讓我有一個關於使用累積和分鐘的想法。謝謝。 – ppk

2

這是無關的線程。你已經用「默認填充構造函數」構建了cuads,它填充了許多默認構造元素作爲它的參數。在這種情況下,您已經要求10(origen的大小),並且默認構建的整數是零,所以您有一個包含10個零的向量。然後你將你的力量值推到它的後面,製作20個元素。

只需使用cuads的普通vector<T>默認構造函數,錯誤就會消失。