我的问题是关于创建对象时传入的值。进行调试,很明显'id','cont','size'和'nthreads'的值不知何故丢失了。我在互联网上搜索并找到链接https://stackoverflow.com/questions/1151582教如何传递对象并让方法调用他。它一直有效,直到方法'run()',在创建对象时传递的值丢失。我不知道我是否需要弄乱静态类型以便它不会,但是,我不知道该怎么做。
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);
}
}