1
我目前正在使用boost 1.55.0,我不明白为什么这段代码不起作用。线程同步与boost :: condition_variable
以下代码是与我的程序具有相同问题的简化。小运行完成,但是当它们更大时,线程会一直等待。
boost::mutex m1;
boost::mutex critical_sim;
int total= 50000;
class krig{
public:
float dokrig(int in,float *sim, bool *aux, boost::condition_variable *hEvent){
float simnew=0;
boost::mutex::scoped_lock lk(m1);
if (in > 0)
{
while(!aux[in-1]){
hEvent[in-1].wait(lk);
}
simnew=1+sim[in-1];
}
return simnew;
};
};
void Simulnode(int itrd,float *sim, bool *aux, boost::condition_variable *hEvent){
int j;
float simnew;
krig kriga;
for(j=itrd; j<total; j=j+2){
if (fmod(1000.*j,total) == 0.0){
printf (" .progress. %f%%\n",100.*(float)j/(float)total);
}
simnew= kriga.dokrig(j,sim, aux, hEvent);
critical_sim.lock();
sim[j]=simnew;
critical_sim.unlock();
aux[j]=true;
hEvent[j].notify_one();
}
}
int main(int argc, char* argv[])
{
int i;
float *sim = new float[total];
bool *aux = new bool[total];
for(i=0; i<total; ++i)
aux[i]=false;
//boost::mutex m1;
boost::condition_variable *hEvent = new boost::condition_variable[total];
boost::thread_group tgroup;
for(i=0; i<2; ++i) {
tgroup.add_thread(new boost::thread(Simulnode, i,sim, aux, hEvent));
}
tgroup.join_all();
return 0;
}
奇怪的是,我发现,如果我的地方就是dokrig()内内嵌在simulnode代码(),然后它似乎工作。这可能是锁的范围有问题吗?
有人可以告诉我我错了吗?提前致谢。