-1

My question is regarding the values ​​passed in when creating an object. Making debugging, it is clear that the values ​​of 'id', 'cont', 'size' and 'nthreads' get lost somehow. I searched the internet and found the link https://stackoverflow.com/questions/1151582 teaching how to pass an object and make a method call him. It works until the method 'run ()', where the values ​​passed when creating the object are lost. I do not know if I need to mess with static types so that it does not, however, I am not knowing what to do.

class Section1SimpleBarrierThread {

 Section1SimpleBarrierThread(int id, Section1Counter* cont, int size, int nthreads) {
        this->id = id;
        this->cont = cont;
        this->size = size; 
        this->nthreads = nthreads;
  }

  void* run(void){
    pthread_mutex_lock(&mutex);
    for(int i=0; i<size; i++) {
       cont->shared_cont++;
       if(cont->shared_cont != this->nthreads) {
         //cont.wait();
       } else {
          //cont->shared_cont = 0;
          // cont.notifyAll();
       }  
   }
   pthread_mutex_unlock(&mutex);
  }

  static void* run_helper(void* context){
     return ((Section1SimpleBarrierThread*)context)->run();
  }  
};

while ( (time < TARGETTIME) && (size < MAXSIZE) ){     
j->resetTimer("Section1:Barrier:Simple"); j->startTimer("Section1:Barrier:Simple");

for(int i=0; i<nthreads; i++) { 
thobjectsSimpleBarrierThread[i]= new Section1SimpleBarrierThread(i,cont,size,nthreads);
pthread_create(&thread_id[i],NULL,&Section1SimpleBarrierThread::run_helper,&thobjectsSimpleBarrierThread[i]);

for(int i=0; i<nthreads; i++) {
        pthread_join(thread_id[i],NULL);
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Maybe you have messed up the braces, maybe it was me when I reformatted the text. Anyway you should double-check them... – rodrigo May 01 '12 at 20:25
  • It's all right. One part is the class definition, the other is a part of the code where I use it. Thanks for tidying up the formatting, I fumbled at the time. – Daniel Gariani Rafael May 01 '12 at 20:28
  • It cannot be right because the while part has 3 `{` but only 2 `}`. – rodrigo May 01 '12 at 20:34

1 Answers1

0

'&thobjectsSimpleBarrierThread[i]' - where is this array, and what is its type. The '&' operator here looks very suspicious... Surely thobjectsSimpleBarrierThread already a pointer type because you new()ed it on the line above!

Martin James
  • 24,453
  • 3
  • 36
  • 60