bool checkNeighbors(point currentPoint, grid * theGrid) { int row = currentPoint.getRow(); int column = currentPoint.getColumn(); int currentValue = theGrid->getValue(row,column); int status; if (row != 0) { // Neighbor to the north (Canada) status = checkPosition(row-1, column, currentValue, theGrid); if (status == 99) return true; else if (status == 1) pointQueue.enqueue(point(row-1,column)); } if (row < theGrid->getSize() -1) { // Neighbor to the south (Mexico) status = checkPosition(row+1, column, currentValue, theGrid); if (status == 99) return true; else if (status == 1) pointQueue.enqueue(point(row+1,column)); } if (column != 0) { // Neighbor to the west (Pacific) status = checkPosition(row, column-1, currentValue, theGrid); if (status == 99) return true; else if (status == 1) pointQueue.enqueue(point(row,column-1)); } if (column < theGrid->getSize() -1) { // Neighbor to the east (Atlantic) status = checkPosition(row, column+1, currentValue, theGrid); if (status == 99) return true; else if (status == 1) pointQueue.enqueue(point(row,column+1)); } return false; }
void bfrange::enqueue(int block) { restore(); // both goal positions bfqueue.enqueue(this); if (block) bfqueue.block(); }
void filter( queue<message> &in, queue<filtered_message> &out) { while (true) { try { message msg = in.dequeue(); filtered_message filt_msg = filtered_message(); string filtered_text = msg.text; for (size_t i=0; i < ( sizeof(words)/sizeof(words[0])); i++) { size_t index = 0; while (true) { index = filtered_text.find(words[i], index); if (index == std::string::npos) { break; } filt_msg.ok = false; filtered_text.replace( index, words[i].size(), string(words[i].size(), '*') ); } } filt_msg.text = filtered_text; out.enqueue(filt_msg); } catch (queue_empty_exception) { break; } } }
void range::enqueue(int block) { squeue.enqueue(this); if (block) squeue.block(); }
void detectEdgeNodata::processWindow(dimension_type row, dimension_type col, elevation_type &point, elevation_type *a, elevation_type *b, elevation_type *c) { AMI_err ae; static nodataType prevCell; /* cell on left (gets initialized) */ assert(row>=0); assert(col>=0); /* create window and write out */ ElevationWindow win(a, b, c); fillPit(win); /* fill pit in window */ ae = elevStream->write_item(win.get()); assert(ae == AMI_ERROR_NO_ERROR); /* only interested in nodata in this pass */ if(win.get() != nodata) { prevCell.label = LABEL_UNDEF; return; } if(col == 0) prevCell.label = LABEL_UNDEF; /* no left cell */ /* now check for continuing plateaus */ nodataType *ptarr = getNodataForward(row-1, col-1, nr, nc); /* make sure we use boundary label if appropriate */ cclabel_type crtlabel; crtlabel = (IS_BOUNDARY(row,col,nr, nc) ? LABEL_BOUNDARY : LABEL_UNDEF); for(int i=0; i<4; i++) { if(win.get(i) != win.get()) continue; /* only interesting if same elev */ /* determine label for cell */ cclabel_type label = LABEL_UNDEF; if(i<3) { if(ptarr[i].valid) label = ptarr[i].label; } else { if(prevCell.valid) label = prevCell.label; } /* check for collisions */ if(label != LABEL_UNDEF) { if (crtlabel == LABEL_UNDEF) { crtlabel = label; } else if(crtlabel != label) { /* collision!! */ /* pick smaller label, but prefer nodata */ if(crtlabel==LABEL_BOUNDARY || crtlabel<label) { colTree.insert(crtlabel, label); } else { colTree.insert(label, crtlabel); crtlabel = label; } } } } /* assign label if required */ if(crtlabel == LABEL_UNDEF) { crtlabel = labelFactory::getNewLabel(); } /* write this plateau point to the plateau stream */ nodataType pt; prevCell = pt = nodataType(row, col, crtlabel); nodataQueue->enqueue(pt); /* NODATA_DEBUG *stats << "inserting " << pt << endl; */ nodataStream->write_item(pt); /* save to file for later use */ }
void *operators(void *threadid) { long tid; tid = (long)threadid; printf("Hello World! It's me, operator_thread #%ld!\n", tid); queue require; queue tools; while(true)//for (int i=0; i<10; i++)//change to while(true) { pthread_mutex_lock(&mutex_pause); while (isPaused == 1) { pthread_cond_wait(&cond_pause, &mutex_pause); } //else //{ pthread_mutex_unlock(&mutex_pause); int rand_time=rand() % 10000; usleep(rand_time); pthread_mutex_lock(&M);//lock mutex //critical section if (fix_buffer.size()==0) { cout<<"Empty"<<endl; dead_count++; cout<<"Deadlock due to Empty!!!"<<endl; cout<<"Number of deadlock happened is "<<dead_count<<endl; } while(fix_buffer.size()==0)//wait if fixed size buffer is 0 if (pthread_cond_wait (&C1, &M)) { fprintf (stdout, "pthread_cond_wait: consumer\n"); exit (-1); } if (require.size()==0)//get 1st material { int temp=fix_buffer.dequeue(); pthread_cond_signal (&C); require.enqueue(temp); cout<<"get "<<temp<<"by "<<tid<<endl; cout<<"require: "<<endl; require.show(); } else if (require.size()==1)//get 2nd material { int temp1=fix_buffer.dequeue(); pthread_cond_signal(&C); if (temp1 != require.front()) { require.enqueue(temp1); cout<<"get "<<temp1<<"by "<<tid<<endl; cout<<"require: "<<endl; require.show(); int check_product=require.check_p(); if (output_Q.size()==0) //starting queue no need to check for previous product { int random_tool1; int random_tool2; if(pthread_mutex_trylock(&M1)==0) { while(tools.size()<2) { if (tools.size()==0) { random_tool1=rand() % tool_num + 1; tools.enqueue(random_tool1); cout<<"get tools"<<random_tool1<<" by operator "<<tid<<endl; tools.show(); } else if (tools.size()==1) { random_tool1=rand() % tool_num + 1; if (!(random_tool1==random_tool2)) { tools.enqueue(random_tool1); cout<<"get tools"<<random_tool1<<" by operator "<<tid<<endl; tools.show(); } } } } int product=require.dequeue(); product=product+require.dequeue(); output_Q.enqueue(product); cout<<"output_Q: "<<endl; output_Q.show(); if (product==3) product1_count++; else if (product==4) product2_count++; else product3_count++; cout<<product1_count<<" of product 3 are generated\n"; cout<<product2_count<<" of product 4 are generated\n"; cout<<product3_count<<" of product 5 are generated\n"; } else// if it is not 1st product { if ((!output_Q.recent(check_product)) || (!output_Q.check_entire(check_product))) { dead_count++; cout<<"Deadlock due to recent product is same as new product or different number of products are more than 10 !!!"<<endl; cout<<"Number of deadlock happened is "<<dead_count<<endl; while(fix_buffer.size() >=10) //check if there are many of those products if (pthread_cond_wait (&C1, &M))//producer of the material goes to wait { fprintf (stdout, "pthread_cond_wait: producer\n"); exit (-1); } //for (int i=0;i<2;i++) //{ int put_back=require.dequeue(); fix_buffer.enqueue(put_back); pthread_cond_signal (&C1); cout<<"Prevent Deadlock put : "<<put_back<<" to buffer"<<endl; fix_buffer.show(); cout<<"current size of fix_buffer is "<<fix_buffer.size()<<endl; //} } /*while(!output_Q.recent(product))//if recent is of same kind then wait if (pthread_cond_wait (&C2, &M)) { fprintf (stdout, "pthread_cond_wait: consumer\n"); exit (-1); } while(!output_Q.check_entire(product))// wait if the difference are 10 if (pthread_cond_wait (&C2, &M)) { fprintf (stdout, "pthread_cond_wait: consumer\n"); exit (-1); }*/ else { int random_tool1; int random_tool2; if(pthread_mutex_trylock(&M1)==0) { while(tools.size()<2) { if (tools.size()==0) { random_tool1=rand() % tool_num + 1; tools.enqueue(random_tool1); cout<<"get tools"<<random_tool1<<" by operator "<<tid<<endl; tools.show(); } else if (tools.size()==1) { random_tool1=rand() % tool_num + 1; if (!(random_tool1==random_tool2)) { tools.enqueue(random_tool1); cout<<"get tools"<<random_tool1<<" by operator "<<tid<<endl; tools.show(); } } } int product=require.dequeue(); product=product+require.dequeue(); cout<<"product is "<<product<<" by tools"<<tools.dequeue()<<" and "<<tools.dequeue()<<endl; output_Q.enqueue(product); cout<<"output_Q: "<<endl; output_Q.show(); cout<<"current size of output queue is "<<output_Q.size()<<endl; if (product==3) product1_count++; else if (product==4) product2_count++; else product3_count++; cout<<product1_count<<" of product 3 are generated\n"; cout<<product2_count<<" of product 4 are generated\n"; cout<<product3_count<<" of product 5 are generated\n"; } } }//end of not 1st output }// end of if (temp1 ! = require.front()) else if ((temp1 == require.front())) { dead_count++; cout<<"Deadlock due to both materials are same!!!"<<endl; cout<<"Number of deadlock happened is "<<dead_count<<endl; fix_buffer.enqueue(temp1); pthread_cond_signal (&C1); } } pthread_mutex_unlock ( &M); pthread_mutex_unlock ( &M1); pthread_mutex_unlock ( &M2); pthread_mutex_unlock ( &M3); //non critical section pthread_cond_signal (&C); //}//if pause } pthread_exit(NULL); }
void * generators(void *threadid) { long tid; tid = (long)threadid; printf("Hello World! It's me, generator_thread #%ld!\n", tid); while(true)//for (int i=0; i<10; i++)//change to while(true) { pthread_mutex_lock(&mutex_pause); while (isPaused == 1) { pthread_cond_wait(&cond_pause, &mutex_pause); } //else //{ pthread_mutex_unlock(&mutex_pause); //int rand_time=rand() % 10000; //usleep(rand_time); pthread_mutex_lock(&M);//lock mutex //critical section int material=tid; if ((!fix_buffer.check(tid)) || (!fix_buffer.check_buffer(material)) || (fix_buffer.size() >=10) ) { dead_count++; cout<<"Deadlock due to full or too many of the material in the buffer!!!"<<endl; cout<<"Number of deadlock happened is "<<dead_count<<endl; } while ((!fix_buffer.check(tid)) || (!fix_buffer.check_buffer(material)) || (fix_buffer.size() >=10) )//check if there are many of those products if (pthread_cond_wait (&C, &M))//producer of the material goes to wait { fprintf (stdout, "pthread_cond_wait: producer\n"); exit (-1); } /*int material=tid; while(!fix_buffer.check_buffer(material))// wait if the difference are 10 if (pthread_cond_wait (&C, &M)) { fprintf (stdout, "pthread_cond_wait: consumer\n"); exit (-1); }*/ printf(" thread #%ld produces %ld\n", tid, material); if (fix_buffer.size() >= 10) { cout<<"Full Wait"<<endl; } /*while (fix_buffer.size() >=10)//if its full wait if (pthread_cond_wait ( &C1 , &M)) { fprintf (stdout, "pthread_cond_wait: producer\n"); exit (-1); }*/ fix_buffer.enqueue(material); //pthread_cond_signal (&C1); if (material==1) material1_count++; else if (material==2) material2_count++; else material3_count++; cout<<"current size of fix_buffer is "<<fix_buffer.size()<<endl; fix_buffer.show(); cout<<material1_count<<" of material 1 are generated\n"; cout<<material2_count<<" of material 2 are generated\n"; cout<<material3_count<<" of material 3 are generated\n"; pthread_mutex_unlock (&M); // noncritical section pthread_cond_signal (&C1); } //} pthread_exit(NULL); }