void test2(void) { ExitCondition E(4); LockedQueue Q; std::cout << "----test2" << std::endl; std::cout.flush(); Thread<Consumer> t1(Consumer(E, Q, 10, "consumer 1")); Thread<Consumer> t2(Consumer(E, Q, 10, "consumer 2")); Thread<Producer> t3(Producer(E, Q, 11, 20)); Thread<Producer> t4(Producer(E, Q, 1, 10)); E.wait(); }
//Main function int main(int argc, char const *argv[]) { //define semaphores sem_init(&mutex,0,1); sem_init(&empty,0,MAX_QEQUEST); sem_init(&full,0,0); //initialize the request queue for(int i=0; i<MAX_QEQUEST;i++){ Request* r = (Request*)malloc(sizeof(Request)); r->ID = -1; r->length = -1; REQUEST_ARR[i] = r; } //initialize the producer, the second input is the sleep length pthread_t a[1]; Producer(a,1); //initialize the consumers, the first input is the number of consumers pthread_t b[5]; Consumer(5,b); Free(); return 0; }
std::unique_ptr<clang::ASTConsumer> USRFindingAction::newASTConsumer() { std::unique_ptr<FindDeclarationsConsumer> Consumer( new FindDeclarationsConsumer(symbolName, unifiedSymbolResolutions_, line, column)); return std::move(Consumer); }
int main() { ProducerConsumerMonitor monitor; Producer producer = Producer(&monitor); producer.start(); Consumer consumer = Consumer(&monitor); consumer.start(); Consumer anotherConsumer = Consumer(&monitor); anotherConsumer.start(); producer.waitFor(); consumer.waitFor(); anotherConsumer.waitFor(); }
std::unique_ptr<ASTConsumer> USRFindingAction::newASTConsumer() { std::unique_ptr<NamedDeclFindingConsumer> Consumer( new NamedDeclFindingConsumer); SpellingName = ""; Consumer->SymbolOffset = SymbolOffset; Consumer->OldName = OldName; Consumer->USRs = &USRs; Consumer->SpellingName = &SpellingName; return std::move(Consumer); }
com_ptr<IDataObject> PidlFixture::data_object_from_sandbox() { vector<cpidl_t> pidls = pidls_in_sandbox(); pidl_array<cpidl_t> array(pidls.begin(), pidls.end()); BOOST_REQUIRE_EQUAL(array.size(), 2U); com_ptr<IDataObject> data_object = new CSftpDataObject( numeric_cast<UINT>(array.size()), array.as_array(), sandbox_pidl().get(), Provider(), Consumer()); BOOST_REQUIRE(data_object); return data_object; }
void test1(void) { ExitCondition E(2); LockedQueue Q; std::cout << "----test1" << std::endl; std::cout.flush(); Thread<Consumer> t2(Consumer(E, Q, 10, "consumer")); Thread<Producer> t1(Producer(E, Q, 1, 10)); E.wait(); }
vector<cpidl_t> PidlFixture::pidls_in_sandbox() { CSftpDirectory dir(sandbox_pidl(), Provider(), Consumer()); com_ptr<IEnumIDList> pidl_enum = dir.GetEnum( SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN); vector<cpidl_t> pidls; cpidl_t pidl; while (pidl_enum->Next(1, pidl.out(), NULL) == S_OK) { pidls.push_back(pidl); } return pidls; }
void* routine(void* info) { struct thInfo* thinfo = (struct thInfo*) info; if(isConsumer(&thinfo->flags)) { CriticalAdd(&cCountLock, &cCount, 1); int r=0; while(r == 0 && getAmount(&thinfo->flags) > 0 ) { r = Consumer(thinfo->persist, ConsumerAction, thinfo); } CriticalAdd(&cCountLock, &cCount, -1); } if(isProducer(&thinfo->flags)) { CriticalAdd(&pCountLock, &pCount, 1); int r=0; while(r == 0 && getAmount(&thinfo->flags) > 0 ) { r = Producer(thinfo->persist, ProducerAction, thinfo); } CriticalAdd(&pCountLock, &pCount, -1); } return NULL; }
void WINAPI Consumer(int id) { WaitForSingleObject(empty,INFINITE); Sleep(Rand()*consumer_wait_time); WaitForSingleObject(Mutex,INFINITE); store--; handle_count++; ftime(&tp); ptr=localtime(&(tp.time)); printf("操作次数:%d ",handle_count); printf("消费者%d消费编号%d的产品,仓库产品数:%d\n\n",id,Goods[store].id,store); ReleaseMutex(Mutex); ReleaseSemaphore(full,1,NULL); Sleep(800); tmp ++ ; if(tmp < 2*sum_count ) Consumer(id) ; }
//Main method int main(int argc, char * argv[]) { int numProducers, numConsumers; //Number of producers, consumers bool paused = false; //Flag that determines whether to pause the animation or not int centerX = (WINDOW_WIDTH / 2); //Center coordinates for the Canvas window //Check the command line numProducers = (argc > 1) ? atoi(argv[1]) : 1; numConsumers = (argc > 2) ? atoi(argv[2]) : 2; //Set to max number of producers && consumers if negative, zero values if(numProducers <= 0 || numConsumers <= 0) { std::cout << "Invalid input! Now changing to max default values..." << std::endl; numProducers = numConsumers = 8; } else if(numProducers > 8 || numConsumers > 8) { std::cout << "Too many producers/consumers! Now changing to max default values..." << std::endl; numProducers = numConsumers = 8; } Producer * pro = new Producer[numProducers]; //Array of Producers Consumer * con = new Consumer[numConsumers]; //Array of Consumers srand(time(NULL)); //Seed the random number generator (to make random colors) //Fill the arrays of Producers and Consumers with Producer and Consumer objects for(unsigned long i = 0; i < numProducers; i++) { pro[i] = Producer(sharedBuffer, i, queueDisplay); } for(unsigned long j = 0; j < numConsumers; j++) { con[j] = Consumer(sharedBuffer, j, queueDisplay); } //Pause the animation if space bar is pressed queueDisplay.bindToButton(TSGL_SPACE, TSGL_PRESS, [&paused]() { paused = !paused; }); //Fire up the visualization queueDisplay.start(); //Fire up the Legend legendDisplay.start(); queueDisplay.setBackgroundColor(WHITE); legendDisplay.setBackgroundColor(WHITE); int colorChanger = 0; //Changes color on Legend //Draw the Legend for(int i = 0; i < 8; i++) { legendDisplay.drawCircle(30, (40 * (i + 1) - 10), 15, 32, Colors::highContrastColor(i)); legendDisplay.drawText("is Producer " + to_string(i), 60, (40 * (i + 1)), 15, BLACK); } legendDisplay.drawText("Consumers are on the right side ", 60, 360, 15, BLACK); //Start up the pthreads for Producers and Consumers for(int k = 0; k < numProducers; k++) { pro[k].start(); } for(int l = 0; l < numConsumers; l++) { con[l].start(); } //Drawing loop while(queueDisplay.isOpen()) { // queueDisplay.sleepFor(0.5); //Slow down the animation queueDisplay.sleep(); //If we haven't paused the animation... if(!paused) { for(int m = 0; m < numProducers; m++) { //Draw the Producers onto the Canvas pro[m].draw(queueDisplay); } for(int n = 0; n < numConsumers; n++) { //Draw the Consumers onto the Canvas con[n].draw(queueDisplay, WINDOW_WIDTH); } display(queueDisplay, centerX, sharedBuffer); //Show changes } legendDisplay.drawCircle(30, 360, 15, 32, Colors::highContrastColor(colorChanger)); //Consumer on Legend colorChanger++; } //Now join them for(int o = 0; o < numProducers; o++) { //Join the pthreads for the Producers pro[o].join(); } for(int p = 0; p < numConsumers; p++) { //Join the pthreads for the Consumers con[p].join(); } //Cleanup the Canvas and free up memory delete [] pro; delete [] con; pro = NULL; con = NULL; //Hanging process may have something to do with this.... queueDisplay.wait(); if(legendDisplay.isOpen()) { //Close up the Legend Canvas if it's still open, else call wait(). legendDisplay.stop(); } else { legendDisplay.wait(); } return 0; }
int main() { //Calka z sinusa cout << "CALKA Z SINUSA" << endl; double result = 0.0; TworzenieWatkow calka(0, M_PI, &result); boost::thread thread(calka); cout << "Hardware concurrency: " << boost::thread::hardware_concurrency << endl; cout << "Thread ID" << thread.get_id() << endl; thread.join(); cout << "result" << result << endl; cout << "DONE!"; getchar(); //------------------------------------- //Synchronizacja cout << "SYNCHRONIZACJA" << endl; mutex characterMutex; string lancuch; lancuch.resize(CHARACTERS_TO_WRITE); thread_group threads; for (unsigned int i = 0; i < NUMBER_OF_THREADS; ++i) threads.create_thread(Watek('a'+i,lancuch,characterMutex)); threads.join_all(); cout << lancuch << endl; cout << "DONE!"; getchar(); //---------------------------------------- //Zmienne warunkowe cout << "ZMIENNE WARUNKOWE"<<endl; mutex dataMutex; Monitor<char>container(dataMutex); thread_group conditional; mutex consoleMutex; for (int i = 0; i < 5; i++) { conditional.create_thread(Consumer(container,i,consoleMutex)); conditional.create_thread(Producer(container,i,consoleMutex)); } conditional.join_all(); cout << "DONE!"; getchar(); //------------------------------------ //Pociagi/semafory cout << "STACJA KOLEJOWA"<<endl; Peron p1("p1"), p2("p2"), p3("p3"); Train tr1 = Train("T1", p2, p1); boost::thread t1(tr1); boost::thread t2(Train("T2", p2, p3)); boost::thread t3(Train("T3", p2, p1)); boost::thread t4(Train("T4", p2, p3)); t1.join(); t2.join(); t3.join(); t4.join(); cout << "DONE"<<endl; getchar(); cout << "FILOZOFOWIE" << endl; Semafor eating(PHILOSOPHERS - 1); Fork* forks[5]; thread_group philosophers; for (int i = 0; i<PHILOSOPHERS; i++) { forks[i] = new Fork(i); } for (int i = 0; i<PHILOSOPHERS; i++) { philosophers.create_thread(Philosopher(i, forks[i], forks[(i + (PHILOSOPHERS -1)) % PHILOSOPHERS], eating)); } philosophers.join_all(); cout << "DONE"<<endl; getchar(); cout << "TASK 1" << endl; mutex tConsoleMutex; Task1 taskOne(tConsoleMutex,0); Task1 taskTwo(tConsoleMutex,1); boost::thread th1(taskOne); boost::thread th2(taskTwo); th1.join(); th2.join(); cout << "DONE" << endl; getchar(); cout << "TASK 2" << endl; boost::mutex task2Mutex; Task2 task2DataGenerator(100); boost::thread task2Thread(task2DataGenerator); boost::unique_lock<mutex>task2Lock(task2Mutex); task2DataGenerator.task2isGeneratingData->wait(task2Lock); for (int i = 0; i < 100; i++)cout << task2Array[i] << endl; cout << "DONE" << endl; getchar(); cout << "TASK 3" << endl; thread_group task3Group; Semafor task3Semafor(1); Semafor task3Max3ThreadsSemafor(3); for (int i = 0; i < 10; i++) { task3Group.create_thread(Task3(task3Max3ThreadsSemafor,task3Semafor,i)); } task3Group.join_all(); cout << "DONE" << endl; getchar(); cout << "SMOLKA EXAM"<<endl; cout << "TASK 1"<<endl; boost::mutex sConsoleMutex; STask1 st1(sConsoleMutex); STask1 st2(sConsoleMutex); boost::thread sTask1Thread(st1); boost::thread sTask1Thread2(st2); sTask1Thread.join(); sTask1Thread2.join(); cout << "DONE" << endl; getchar(); cout << "TASK 2" << endl; sN = 75; boost::thread sTask2Thread(sTask2); boost::mutex sTask2Mutex; boost::unique_lock<mutex>sTask2Lock(sTask2Mutex); sTask2Condition.wait(sTask2Lock); for (int i = 0; i < 75; i++) { cout<< sTask2Array[i]<<endl; } getchar(); return 0; }
int main(int argc, char** argv) { std::string uri; int pipelineSize = 1; bool mustBeFresh = false; bool isVerbose = false; time::milliseconds interestLifetime(4000); int opt; while ((opt = ::getopt(argc, argv, "fl:p:vh")) != -1) { switch (opt) { case 'f': mustBeFresh = true; break; case 'l': { auto lifetime = ::atoi(optarg); if (lifetime >= 0) interestLifetime = time::milliseconds(lifetime); break; } case 'p': { auto count = ::atoi(optarg); if (count > pipelineSize) pipelineSize = count; break; } case 'v': isVerbose = true; break; case 'h': usage(argv[0]); return 0; default: usage(argv[0]); return 1; } } if (optind < argc) { uri = argv[optind]; } if (uri.empty()) { usage(argv[0]); return 1; } Consumer().run(uri, pipelineSize, mustBeFresh, interestLifetime, isVerbose); return 0; }
RPCSub::RPCSub (InfoSub::Source& source) : InfoSub (source, Consumer()) { }