blocking_job takeJob() throw(QueueBreak) { MutexGuard lock(&mutex_); while(queue_.size()==0 && running_) { cond_.wait(mutex_);//release mutex and block here } //printf("thread wake: %s %d, and should do some job assignment here:%d\n", //Thread::ThreadName().c_str(), Thread::GetTid(), count); if(!running_) { throw QueueBreak("QueueBreak"); } blocking_job job = 0; if(queue_.size()) { job = queue_.front(); queue_.pop_front(); } if(queue_.size()) cond_.notify(); return job; }
void addJob(const blocking_job &job) { MutexGuard lock(&mutex_); queue_.push_back(job); cond_.notify(); }
void *get(void *t) { for(int i = 0; i < 20; i++) cout << "-- " << myqueue.pop(1000) << endl; //cout << "-- " << myqueue.pop() << endl; pthread_exit(NULL); return NULL; }
void start_thread() { thread_group thrgrp; for (size_t i = 0;i < 4; ++i) { thrgrp.launch(boost::bind( &simple_engine_test::thread, this, i)); } thrgrp.join(); ASSERT_EQ(queue.size(), 0); }
void thread(size_t cpuid) { while(1) { std::pair<size_t, bool> job = queue.try_dequeue(); if (job.second == false) { bool ret = try_terminate(cpuid, job); if (ret == true) break; if (ret == false && job.second == false) continue; } task(job.first); } }
void *add(void *t) { int i = (int) t; int limit = i + 10; for(; i < limit; i++) { if(i % 2 == 0) Sleep(2); myqueue.push(i); } pthread_exit(NULL); return NULL; }
bool try_terminate(size_t cpuid, std::pair<size_t, bool> &job) { job.second = false; numactive.dec(); cons.begin_done_critical_section(cpuid); job = queue.try_dequeue(); if (job.second == false) { bool ret = cons.end_done_critical_section(cpuid); numactive.inc(); return ret; } else { cons.cancel_critical_section(cpuid); numactive.inc(); return false; } }
void add_task_local(size_t i) { queue.enqueue(i); if (numactive.value < 4) cons.cancel(); }
void foo(){ x.write(42); }