void f() { typedef std::chrono::system_clock Clock; typedef std::chrono::milliseconds milliseconds; L1 lk(m0); assert(test2 == 0); test1 = 1; cv.notify_one(); Clock::time_point t0 = Clock::now(); while (test2 == 0 && cv.wait_for(lk, milliseconds(250)) == std::cv_status::no_timeout) ; Clock::time_point t1 = Clock::now(); if (runs == 0) { assert(t1 - t0 < milliseconds(250)); assert(test2 != 0); } else { assert(t1 - t0 - milliseconds(250) < milliseconds(5)); assert(test2 == 0); } ++runs; }
void post() { std::lock_guard<std::mutex> lock(mutex_); //++count_; work_waiting = true; condition_.notify_one(); }
void _process(int id) { std::unique_lock<std::mutex> lock(io_datas[id]->mtx); if(io_datas[id]->empty()) { io_datas[id]->cond.wait(lock); /* In case that the queue is not used at the begining and it shall * make dequeue operation failed. */ if(io_datas[id]->empty()) { lock.unlock(); return; } } IoRef io = io_datas[id]->dequeue(); lock.unlock(); auto time_beg = std::chrono::steady_clock::now(); handle_io(io->offset, io->c); auto time_end = std::chrono::steady_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::microseconds>(time_end - time_beg).count(); io_counter_per_thread[id]++; perf.consumed_time += duration; perf.io_finished++; prod_cond.notify_one(); }
int main() { { L1 lk(m0); std::thread t(f); assert(test1 == 0); while (test1 == 0) cv.wait(lk); assert(test1 != 0); test2 = 1; lk.unlock(); cv.notify_one(); t.join(); } test1 = 0; test2 = 0; { L1 lk(m0); std::thread t(f); assert(test1 == 0); while (test1 == 0) cv.wait(lk); assert(test1 != 0); lk.unlock(); t.join(); } }
void f() { L1 lk(m0); assert(test2 == 0); test1 = 1; cv.notify_one(); cv.wait(lk, Pred(test2)); assert(test2 != 0); }
void Worker::post(const QueryPtr& query) { { std::lock_guard<std::mutex> guard(mutex_); assert(follower_query_ == nullptr); follower_query_ = query; } cond_.notify_one(); }
/*供messagemanager调用*/ bool ThreadPool::addWork() { /*工作线程数加一*/ std::lock_guard<std::mutex> lck(mtx); if(isFull()) throw Exception(ERR_BAD,"线程池已经用完,无法提供ThreadPool::addWork()调用"); freeThreadNumber--; cond_var.notify_one();//唤醒一个线程,并从消息队列取消息处理 return true; }
void Worker::post_to_queue(const QueryPtr& query) { if (!working_) return; { std::lock_guard<std::mutex> guard(mutex_); query_queue_.push_back(query); } cond_.notify_one(); }
HttpClient::~HttpClient() { if (s_requestQueue != nullptr) { { std::lock_guard<std::mutex> lock(s_requestQueueMutex); s_requestQueue->pushBack(s_requestSentinel); } s_SleepCondition.notify_one(); } s_pHttpClient = nullptr; }
void Worker::stop() { working_ = false; if (thread_ != nullptr) { cond_.notify_one(); thread_->join(); thread_.reset(); } mysql_close(&conn_); }
void WorkHard() { m_SectorClear = true; std::mutex mutex; std::unique_lock<std::mutex> lock(mutex); while(true) { if(g_Bell.wait_for(lock, std::chrono::seconds(5)) == std::cv_status::timeout) std::this_thread::sleep_for(std::chrono::seconds(10)); else { NotifyFellows(); g_Door.notify_one(); std::cout << "Hello Great Manager, your slaves are ready to serve you!\n" << std::endl; } } }
//Add a get task to queue void HttpClient::send(HttpRequest* request) { if (false == lazyInitThreadSemphore()) { return; } if (!request) { return; } request->retain(); if (nullptr != s_requestQueue) { s_requestQueueMutex.lock(); s_requestQueue->pushBack(request); s_requestQueueMutex.unlock(); // Notify thread start to work s_SleepCondition.notify_one(); } }
void f() { typedef std::chrono::system_clock Clock; typedef std::chrono::milliseconds milliseconds; L1 lk(m0); assert(test2 == 0); test1 = 1; cv.notify_one(); Clock::time_point t0 = Clock::now(); bool r = cv.wait_for(lk, milliseconds(250), Pred(test2)); Clock::time_point t1 = Clock::now(); if (runs == 0) { assert(t1 - t0 < milliseconds(250)); assert(test2 != 0); } else { assert(t1 - t0 - milliseconds(250) < milliseconds(5)); assert(test2 == 0); } ++runs; }
void f() { L1 lk(m0); assert(test2 == 0); test1 = 1; cv.notify_one(); Clock::time_point t0 = Clock::now(); Clock::time_point t = t0 + Clock::duration(250); while (test2 == 0 && cv.wait_until(lk, t) == std::cv_status::no_timeout) ; Clock::time_point t1 = Clock::now(); if (runs == 0) { assert(t1 - t0 < Clock::duration(250)); assert(test2 != 0); } else { assert(t1 - t0 - Clock::duration(250) < Clock::duration(5)); assert(test2 == 0); } ++runs; }
void f() { L1 lk(m0); assert(test2 == 0); test1 = 1; cv.notify_one(); Clock::time_point t0 = Clock::now(); Clock::time_point t = t0 + Clock::duration(250); bool r = cv.wait_until(lk, t, Pred(test2)); Clock::time_point t1 = Clock::now(); if (runs == 0) { assert(t1 - t0 < Clock::duration(250)); assert(test2 != 0); assert(r); } else { assert(t1 - t0 - Clock::duration(250) < Clock::duration(2)); assert(test2 == 0); assert(!r); } ++runs; }
// Callback method that stores its results on the global variable. // Used for async operations. Call cv.wait(m) after executing an // async operation and then check the returnedValues. // // Do NOT call cv.wait(m) for synchronous operations. The // condition will never be signalled. void storeCallbackArgAndNotify(vector<folly::dynamic> args) { std::lock_guard<std::recursive_mutex> lock(m); returnedValues = args; cv.notify_one(); }
void signal_me() { std::this_thread::sleep_for(std::chrono::milliseconds(500)); cv.notify_one(); }