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; } } }
/* return a pointer to three plateauType structures, starting at location i,j. caller should check valid field in returned structs. */ nodataType * detectEdgeNodata::getNodataForward(dimension_type i, dimension_type j, dimension_type nr, dimension_type nc) { bool ok; static nodataType ptarr[3]; /* return value */ nodataType pt; ok = nodataQueue->peek(0, &pt); while(ok && (pt.i < i || (pt.i==i && pt.j<j))) { nodataQueue->dequeue(&pt); /* past needing this, so remove */ ok = nodataQueue->peek(0, &pt); } if(ok && pt.i == i && pt.j == j) { nodataQueue->dequeue(&pt); /* found it, so remove */ ptarr[0] = pt; } else { ptarr[0].invalidate(); } /* locate next two, if possible */ for(int kk=0,k=1; k<3; k++) { ok = nodataQueue->peek(kk, &pt); if(ok && pt.i == i && pt.j == j+k) { ptarr[k] = pt; kk++; /* found something, so need to peek further forward */ } else { ptarr[k].invalidate(); } } #if(0) cout << "request at " << i << "," << j << " returns: " << ptarr[0] << ptarr[1] << ptarr[2] << endl; nodataQueue->peek(0, &pt); cout << "queue length = " << nodataQueue->length() << "; head=" << pt << endl; #endif return ptarr; }
// Fill the staging area with a minimal chunk of input ranges. int mergestream::prime() { if (dbg & 4) printf("#entering mergestream::prime()\n"); if (!empty()) return 1; int brkok = 1; // is it OK to break after the last // VBOX that was added to the stage? int needheight = -1; // minimum acceptable height of the // chunk being constructed on stage // If the range at the head of any queue is breaking, // deal with it first. if (squeue.more() && squeue.current()->breaking()) enqueue(squeue.dequeue()); else if (bfqueue.more() && (bfqueue.current()->breaking() || (bfqueue.serialno() < squeue.serialno()))) enqueue(bfqueue.dequeue()); else if (ufqueue.more() && (ufqueue.current()->breaking() || (ufqueue.serialno() < squeue.serialno()))) enqueue(ufqueue.dequeue()); else while (squeue.more()) { // Fill the stage with enough ranges to be a valid chunk. range *r = squeue.dequeue(); if (r->isvbox()) { // VBOX if (dbg & 16) printf("#VBOX: !empty: %d; brkok: %d; vsince: %d\n", !empty(), brkok, currpage->vsince); if (!empty() // there's something there && brkok // it's OK to break here && currpage->vsince >= 2 // enough stream has gone onto this page && rawht() >= needheight // current need has been satisfied ) { // the stage already contains enough // ranges, so this one can wait r->enqueue(); break; } else { if (r->rawht() > 0) { ++currpage->vsince; brkok = r->brkafter(); } enqueue(r); } } else if (r->isnested() || r->issp()) { // US, SP if (!empty() && rawht() >= needheight) { // enough already, wait r->enqueue(); break; } currpage->vsince = 0; enqueue(r); if (height() >= needheight) break; } else if (r->isneed()) { // NE if (!empty() && rawht() >= needheight) { // not currently working on an unsatisfied NEed r->enqueue(); break; } // deal with overlapping NEeds needheight = rawht() + max(needheight - rawht(), r->needht()); enqueue(r); } else if (r->forceflush() == NO) { enqueue(r); } else if (r->forceflush() == YES) { currpage->vsince = 0; if (!empty()) { // ready or not, r must wait r->enqueue(); break; } enqueue(r); break; } else ERROR "unexpected %s[%s] in prime(), line %d\n", r->type_name(), r->headstr(), r->lineno() FATAL; } return more(); // 0 if nothing was staged }
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); }