c++ - Producer/Consumer design - Share queue variable across threads in Qt -



c++ - Producer/Consumer design - Share queue variable across threads in Qt -

i have tried create concurrent queue class, in producer-consumer pattern.

i based class off http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html

in main thread, called next 2 methods using qt::concurrent

qfuturewatcher<void> *loadwatcher; loadwatcher = new qfuturewatcher<void>(); qstring filename("myfile"); qfuture<void> future = qtconcurrent::run(this, &eraserbatch::loadtiles,filename); loadwatcher->setfuture(future); qfuturewatcher<bool> *procwatcher; procwatcher = new qfuturewatcher<bool>(); qfuture<bool> procfuture = qtconcurrent::run(this, &eraserbatch::processtile); procwatcher->setfuture(procfuture);

so loadtiles producer, , processtile consumer.

in loadtiles, have loop loading tiles so:

for(int =0; < nxblocks; i++) { for(int j = 0; j < nyblocks; j++) { tile *ipi = new tile(filename); sleep(1); qdebug()<<"tile pushed queue:"<<i<<" "<<j << " thread id:"<<this->thread(); this->tiles->push(ipi); } }

and in consumer method, have following:

tile *tile; this->tiles->wait_and_pop(tile); while(!tiles->empty()) { qdebug()<<"tile popped queue:"<<tile->tileposx<<" "<<tile->tileposy<< " thread id: " << this->thread(); sleep(5); tiles->wait_and_pop(tile); }

this seems force of tiles queue, seems consumer never makes while loop, because pop first tile, , queue empty.

so design flaw - guess need know how tell producer wait tiles added queue, , maintain waiting until of them done. need pop first tile outside while loop setup info variables used in loop, , allocate memory , suhc, dont want on , over. whats best way set up?

in scenario producer needs tell consumer no more info coming. typically achieved having close() method on queue. consumer test become:

while (!tiles->empty() || !tiles->closed()) { ... blocking consume }

c++ multithreading qt thread-safety

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -